简体   繁体   English

替换img属性

[英]Replace img attribute

I have the following img tag that is part of a larger HTML page. 我有以下img标签,它是一个较大的HTML页面的一部分。 This is the first image tag in the document. 这是文档中的第一个图像标签。 I would like to change the first image (white.png) with the data-original attribute. 我想使用data-original属性更改第一张图片(white.png)。

<img width="676" height="450"
     src="http://somewebsite/white.png"
     data-original="http://somewebsite/shutterstock_197488871-676x450.jpg"
     class="lazy attachment-post-thumbnail wp-post-image" alt="(Shutterstock*)"
/>

Here is the HTML 这是HTML

  <div style="font-size: 12pt; color: #ccc; margin-top: 5px; margin-bottom: 5px;"><span id="author">Natural News</span> | <span>Sat, 16 Aug 2014 13:06:21 PM</span>
</div>
<img width="676" height="450" src="http://somewebsiteimages/white.png" data-original="http://somewebsite/shutterstock_197488871-676x450.jpg" class="lazy attachment-post-thumbnail wp-post-image" alt="(Shutterstock*)" />
<noscript>
    <img width="676" height="450" src="http://somewebsite/shutterstock_197488871-676x450.jpg" class="attachment-post-thumbnail wp-post-image" alt="(Shutterstock*)" />
</noscript>

To get the image, you can use one of these: 要获取图像,可以使用以下方法之一:

var img = document.images[0];
var img = document.getElementsByTagName('img')[0];

And to change it, one of 为了改变它,其中之一

img.src = img.getAttribute('data-original'); // old way
img.src = img.dataset.original;              // new way

You can do : 你可以做 :

var workingImage = $("img").first();
workingImage.attr("src",workingImage.attr("data-original"));

This search for the first image. 此搜索第一个图像。 Then, it changes the source by the attribute. 然后,它通过属性更改源。

The implementation I chose was this 我选择的实现是

$(document).ready(function () {
    $('img')
    .first()
    .attr('src', $('img').first().data('original'));
});

I added a document ready function and create an anonymous function in that to do the work. 我添加了一个文档就绪函数,并在其中创建了一个匿名函数来完成工作。 First I selected all of the 'img' elements and then using the 'first()' method we limit our method to just the first img element in the document. 首先,我选择了所有“ img”元素,然后使用“ first()”方法将方法限制为仅文档中的第一个img元素。 Then we change the 'src' attribute of the image to the value of the 'original' location on the same 'img' element. 然后,将图像的“ src”属性更改为同一“ img”元素上“原始”位置的值。

If you could refine the selector to use a class of the image like this you may also have better results 如果您可以优化选择器以使用这样的图像类,则可能还会得到更好的结果

$('img.wp-post-image')

You could also change the jQuery selector to 您也可以将jQuery选择器更改为

$('img.wp-post-image:first')

and then you could remove the .first() function call. 然后您可以删除.first()函数调用。 Which would leave you with this in the .ready function: 这将使您在.ready函数中具有以下功能:

$('img.wp-post-image:first').attr('src', $('img.wp-post-image:first').data('original'));

references: jQuery :first selector jQuery .data() method 参考: jQuery:第一个选择器 jQuery .data()方法

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

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