无法获得最接近图像的textarea的id

san*_*hra -1 jquery

这是我的HTML:

<img src="Image/icons/preview.png" alt="Preview" id="imgPreviewHeader" onclick="ViewHtml(this.id);" />
<textarea rows="30" cols="22" id="txtHeader" class="editor"></textarea>`
Run Code Online (Sandbox Code Playgroud)

JavaScript代码:

$("#" + imgPreviewHeader).closest('textarea.editor').ckeditorGet().execCommand('source');
Run Code Online (Sandbox Code Playgroud)

有谁能够帮我..?

Aru*_*hny 5

你需要在这里使用.next()因为textarea是img元素的下一个兄弟..closest()将在祖先元素中查找不匹配兄弟元素的匹配项

//assuming id has the value imgPreviewHeader
$("#" + id).next('textarea.editor').ckeditorGet().execCommand('source'); 
Run Code Online (Sandbox Code Playgroud)

$("#imgPreviewHeader").next('textarea.editor').ckeditorGet().execCommand('source');
Run Code Online (Sandbox Code Playgroud)