请帮我解决这个问题

时间:2013-01-25 06:41:08

标签: javascript jquery blogger

这是我的剧本:

function createSummaryAndThumb(pID) {
var div = document.getElementById(pID);
var imgtag = "";
var img = div.getElementsByTagName("img");
var summ = summary_noimg;
if(img.length >= 1) {
    imgtag = '<span class="thumbnail pull-left"><img src="' + img[0].srcreplace('/s640/', '/s' + image_size + '/') + '" width="' + img_thumb_width + 'px" height="' + img_thumb_height + 'px"/></span>';
    summ = summary_img;
}

var summary = imgtag + '<p>' + removeHtmlTag(div.innerHTML, summ) + '</p>';
div.innerHTML = summary;
}

上面的脚本将用/ image_size替换/ s640。但是我能做什么才能改变'640'部分的任何值。我试过了,但它不起作用 -

function createSummaryAndThumb(pID) {
var div = document.getElementById(pID);
var imgtag = "";
var img = div.getElementsByTagName("img");
var summ = summary_noimg;
if(img.length >= 1) {
    if (uh>img_thumb_width) imgtag = '<span class="thumbnail pull-left"><img src="' + img[0].src.replace('/s' + uh + '/', '/s' + img_thumb_width + '/') + '" width="' + img_thumb_width + 'px" height="' + img_thumb_height + 'px"/></span>';
    summ = summary_img;
}

var summary = imgtag + '<p>' + removeHtmlTag(div.innerHTML, summ) + '</p>';
div.innerHTML = summary;
}

2 个答案:

答案 0 :(得分:2)

您需要将内部数字与以下内容匹配:

replace(/\/s[0-9]+\//, '/s' + image_size + '/')

以下是一个示例:http://jsfiddle.net/nfDea/

另一种选择是仅捕获开头和结尾,以便连接:

replace(/(\/s)[0-9]+(\/)/, "$1" + image_size + "$2");

http://jsfiddle.net/nfDea/1/

如果您实际上是指一个名为uh的变量,那么您需要使用以下内容:

var re = new RegExp("/s(" + uh + ")/");
a = a.replace(re, "/s" + image_size + "/");

http://jsfiddle.net/nfDea/4/

var re = new RegExp("(/s)" + uh + "(/)");
a = a.replace(re, "$1" + image_size + "$2");

http://jsfiddle.net/nfDea/3/

答案 1 :(得分:-1)

var reg = new RegExp('/s' + uh);
...
img[0].src.replace(reg, '/s' + img_thumb_width + '/')