简体   繁体   English

jQuery在.each内部更改图像属性

[英]jQuery Change Image attributes inside .each

I have a textarea when I paste HTML code, and then a button that loads below the textarea all of the images from that HTML code and it's respective alts and titles. 粘贴HTML代码时,我有一个textarea,然后是一个按钮,该按钮将从HTML代码中加载所有图像及其相应的替代项和标题,并在文本区域下方加载。 The idea is to easily change each alt and title for each image. 想法是轻松更改每个图像的每个alt和标题。 But I can't seem to get it working. 但是我似乎无法正常工作。

Here is the codepen: https://codepen.io/anon/pen/LzVzbd?editors=1010 这是codepen: https ://codepen.io/anon/pen/LzVzbd ? editors = 1010

Insert this example into the textarea, and then click Search. 将此示例插入文本区域,然后单击“搜索”。 The idea is to replace the title and alt in the inputs, and then click update, and the code in the textarea updates. 想法是替换输入中的标题和替代,然后单击更新,并在文本区域中更新代码。

<div>
   <img src="https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png">
</div>

EDIT: This is just a really simple example, The problem is that the people who going to use this paste whole html pages in the textarea. 编辑:这只是一个非常简单的示例,问题是要使用此功能的人将整个html页面粘贴到textarea中。 So the images can be wrapped around table , div , a , p , etc... I'm going to update my question with this information. 因此,可以将图像包装在tabledivap等周围……我将使用此信息来更新我的问题。 Here is a more real life example: 这是一个更真实的示例:

<tr valign="top"><td valing="top">
        <a href="https://stackoverflow.com" style="text-decoration:none; border:0;">
        <img src="https://i.stack.imgur.com/oURrw.png" border="0" style="width: 100%;  max-width: 100%; margin: 0px; padding: 0px; vertical-align:top; display:block" alt="Stack1" title="Stack1" /></a>
        </td>
        </tr>
        <tr valign="top"><td valing="top">
        <a href="https://stackoverflow.com" style="text-decoration:none; border:0;">
        <img src="https://i.stack.imgur.com/oURrw.png" border="0" style="width: 100%;  max-width: 100%; margin: 0px; padding: 0px; vertical-align:top; display:block" alt="Stack2" title="Stack2" /></a>
        </td>
        </tr>
        <tr valign="top"><td valing="top">
        <a href="https://stackoverflow.com" style="text-decoration:none; border:0;">
        <img src="https://i.stack.imgur.com/oURrw.png" border="0" style="width: 100%;  max-width: 100%; margin: 0px; padding: 0px; vertical-align:top; display:block" alt="Stack3" title="Stack3" /></a>
        </td>
        </tr>

Get the jQuery html object of your image list $('.image_list') , update that using jQuery, then insert the html value of that into the textarea. 获取图像列表$('.image_list')的jQuery html对象,使用jQuery更新该对象,然后将其html值插入文本区域。 So, after the tags update, do: 因此,在更新标签后,请执行以下操作:

Edited to fix a problem 编辑以解决问题

var html = "<div>";
$('.image_list img').each(function(){
  html += $(this)[0].outerHTML;
});
html += "</div>";
$('.before_text.replace').val(html);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM