Javascript 如何在整个字符串上使用筛选器?

Javascript 如何在整个字符串上使用筛选器?,javascript,jquery,html,filter,selector,Javascript,Jquery,Html,Filter,Selector,我有一个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

我有一个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> 
//有效
//不起作用
我是舒尔$(cnt)。整个cnt中的过滤器外观

因此,问题是,当被视为DOM时,如何筛选标记的属性,该属性在字符串中的位置不在根中?

。filter
筛选在jQuery对象中包装(选中)的实际元素。要深入DOM树,您需要使用:

或更短:

$("img[src$='"+pic+"']", cnt). ...

首先,我也尝试了查找,但它不起作用。也许您可以告诉我如何使用find通过给定src属性来获取cnt字符串中第三个img的alt属性。
$("img[src$='"+pic+"']", cnt). ...