[英]how to modify the value of src attribute of <img> tag using jquery or javascript
I am parsing a html string fragment and changing the src attribute of image tags. 我正在解析html字符串片段并更改图像标签的src属性。 (ie replacing paths that start with ".."
(即替换以“ ..”开头的路径
var hstr = $.parseHTML(str);
$(hstr).find('img').each(function(e){
var srcvalue = $(this).attr('src');
srcvalue = srcvalue.replace(/../gi, "");
$(this).attr('src', srcvalue);
});
then setting the contents of a div element by appending the result 然后通过附加结果来设置div元素的内容
document.getElementById('#section').append($(hstr));
but it is not working... can anyone tell me what is going wrong ? 但是它不起作用...谁能告诉我出了什么问题?
Dont mess up pure JS DOM and Jquery. 不要搞乱纯JS DOM和Jquery。 Use
$('#section').append($(hstr))
使用
$('#section').append($(hstr))
You should append the manipulated object, you are creating another object, also DOMElement object doesn't have append
method. 您应该追加操作对象,创建另一个对象,DOMElement对象也没有
append
方法。
$(hstr).find('img').each(function(e){
// ...
}).appendTo('#section');
Using prop
method: 使用
prop
方法:
$(hstr).find('img').prop('src', function(index, srcValue) {
return srcValue.replace(/../g, "");
}).appendTo('#section');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.