1

我正在解析一个 html 字符串片段并更改图像标签的 src 属性。(即替换以“..”开头的路径

var hstr = $.parseHTML(str);
$(hstr).find('img').each(function(e){
var srcvalue = $(this).attr('src');
srcvalue = srcvalue.replace(/../gi, "");
$(this).attr('src', srcvalue);
});

然后通过附加结果来设置 div 元素的内容

document.getElementById('#section').append($(hstr));

但它不工作......谁能告诉我出了什么问题?

4

3 回答 3

3

不要搞乱纯 JS DOM 和 Jquery。利用$('#section').append($(hstr))

于 2013-03-06T19:21:14.867 回答
2

不要与 Jquery (#ID)选择器混淆,使用任何一个

$('#section').append($(hstr));

或(在 DOM 元素中不需要#和使用 appendChild

document.getElementById('section').appendChild($(hstr));
于 2013-03-06T19:22:48.820 回答
1

您应该附加被操纵的对象,您正在创建另一个对象,DOMElement 对象也没有append方法。

$(hstr).find('img').each(function(e){
    // ...
}).appendTo('#section');

使用prop方法:

$(hstr).find('img').prop('src', function(index, srcValue) {
    return srcValue.replace(/../g, "");
}).appendTo('#section');
于 2013-03-06T19:21:07.573 回答