在附加元素之前如何解析它的内容? (jQuery)
我有一个 html 字符串,我想直接转储到一个元素中。这个 html 字符串包含一些标签,我需要在它们的“src”属性前面添加一个路径。
目前,我正在做这样的事情:
// note, htmlString is coming in from an external xml file...
var htmlString = "<img src='img1.jpg'/><br/><img src='img2.jpg'/>";
var imgContainer = $('<div />');
imgContainer.append(htmlString);
var prefix = "some/path/to/img/";
imgContainer.find('img').each(function(i, el) {
$(el).attr('src', prefix + $(el).attr('src'));
});
这有效,但我看到非前置路径的资源加载失败。浏览器似乎在创建 imgContainer 元素后立即尝试加载图像,即使它没有附加到文档中。我想避免那些失败的加载。
我想我可以在附加之前解析 htmlString 但在创建元素后只需 $.each() 就很好了...
注意: 我通过问这个问题了解到通过创建一个 jQuery 元素$(htmlString)
立即导致对 htmlString
内的所有资源进行加载尝试。因此,在将 htmlString
打包到 jQuery 对象之前,我必须以某种方式对其进行操作。
I have an html string I'd like to dump directly into an element. This html string contains some tags, and I need to prepend a path to their 'src' attributes.
Currently, I'm doing something like this:
// note, htmlString is coming in from an external xml file...
var htmlString = "<img src='img1.jpg'/><br/><img src='img2.jpg'/>";
var imgContainer = $('<div />');
imgContainer.append(htmlString);
var prefix = "some/path/to/img/";
imgContainer.find('img').each(function(i, el) {
$(el).attr('src', prefix + $(el).attr('src'));
});
This works, but I'm seeing failed resource loads of the non-prepended path. Seems as though the browser is attempting to load the images immediately upon creation of the imgContainer element, even though it's not appended to the document. I'd like to avoid those failed loads.
I suppose I could parse htmlString before appending but it's nice to just $.each() through the element after it's created...
NOTE: I've learned through asking this question that creating a jQuery element via $(htmlString)
immediately causes load attempts on all resources within htmlString
. So, I have to manipulate htmlString
in some way before packing it into a jQuery object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试将
htmlString
解析为 XML,修改它,然后将其附加到 DOM。像这样的东西(不确定
XMLSerializer
是否适用于所有浏览器,我认为它是 IE 中的this.xml
):演示:http://jsfiddle.net/de2dg/3/
You can try to parse the
htmlString
as XML, modify it, and then append it to the DOM.Something like this (Not sure if
XMLSerializer
works in all browsers, I think it'sthis.xml
in IE):DEMO: http://jsfiddle.net/de2dg/3/
为什么不使用正则表达式从字符串中提取图像并使用数组来保存图像并最后附加所有内容?
http://jsfiddle.net/DmKys/
Why don't you extract images with a regex from string and use an array to hold the images and append everything at last?
http://jsfiddle.net/DmKys/
直接在 html 输入中向图像添加一个类,例如“hidden”,并在 css 中使用 display: none; 定义它;它不会首先被绘制。
然后在更改源后在每个循环中删除此类。
编辑:(终于我知道他的意思了)
将属性从 src 更改为 pre_src 之类的任何属性,并在每个循环中使用此属性附加一个新的“src”属性,然后删除 pre_src 。 [删除属性()]
所以资源不会被加载,你可以使用你的每个循环......
Add a class to the images directly in the html-input like "hidden" and define it in css with display: none; that it doesn't get drawn firstly.
Then in your each-loop delete this class after changing the source.
EDIT: (finally I know what he means)
change the attribute from src to anything like pre_src and use this attribute in your each-loop to attach a new "src" attribute and delete pre_src after this. [removeAttr()]
So the resource wont be loaded and you can use your each-loop...