我有一个pictureeditor和一个HTML编辑器。当我在piceditor中更改子行的值时,它应该在html编辑器中更新alt属性。
我用:
function refreshEditorAlt(pic,newalt){
cnt=getEditorContent();
var newcnt = cnt.replace($(cnt).filter("img[src$='"+pic+"']").attr('alt'), newalt);
setEditorContent(newcnt);
}
它一直有效,直到HTML编辑器的源(cnt变量)中的img标记不被例如封闭。 div标签。
//works
<img alt="pic1" src="http://tools.huber-verlag.de/data/18139/1.jpg" />
//does not work
<div><img alt="pic1" src="http://tools.huber-verlag.de/data/18139/1.jpg" /></div>
我是shure $(cnt).filter看着整个cnt。
所以问题是,当被视为DOM时,如何过滤字符串中某个字符但不在根目录中的标记属性?
答案 0 :(得分:1)
.filter
过滤在jQuery对象中包装(选中)的实际元素。要深入了解DOM树,您需要使用.find
:
$(cnt).find("img[src$='"+pic+"']"). ...
或更短:
$("img[src$='"+pic+"']", cnt). ...