سؤال

I got a pictureeditor and a HTML editor. When I change the value of the subline in the piceditor it should update the alt attribute in the html editor.

I use:

function refreshEditorAlt(pic,newalt){
  cnt=getEditorContent();
  var newcnt = cnt.replace($(cnt).filter("img[src$='"+pic+"']").attr('alt'), newalt);
  setEditorContent(newcnt);
}

It works until the img tags in the source (cnt variable) of the HTML editor are not enclosed by e.g. div tags.

//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> 

I was shure $(cnt).filter looks in the whole cnt.

So the question is, how to filter for an attribute of a tag which is somwhere in a string but not in the root, when seen as DOM ?

هل كانت مفيدة؟

المحلول

.filter filters the actual elements, which are wrapped (selected) in jQuery object. To go deep down the DOM tree you need to use .find:

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

Or shorter:

$("img[src$='"+pic+"']", cnt). ...
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top