通过src值查找img标签并在img标签中添加新属性

时间:2018-05-15 09:21:16

标签: javascript jquery html html5 image

如果我在HTML页面中有5张图片。我希望按照src属性值搜索2张图片,并为图片标记添加新属性。

限制是我无法通过任何imgid属性值搜索class代码,我只有src值。在下面的代码中,

我想搜索2个img标签,其中src值类似于 img_src_1 img_src_2 ,并希望在img标签中添加新属性诺信="诺信"

<html>
    <head>
        <script>
        jQuery(document).ready(function(){
            // find img tag by src value and add new attribute nopin="nopin" into this img tag
            var img_src_1 = "https://example.com/image-3.jpg";
            var img_src_2 = "https://example.com/image-5.jpg";

            // find images with src url value is img_src_1 & img_src_2
            var result1 = jQuery('img').find('src', img_src_1);
            var result2 = jQuery('img').find('src', img_src_2);

            // add new attribute nopin="nopin" in both img tag

        });
        </script>
    </head>
    <body>
        <h1>My Gallery Page</h1>
        <p>My Image 1</p>
        <img src="https://example.com/image-1.jpg" width="200px" height="200px">

        <p>My Image 2</p>
        <img src="https://example.com/image-2.jpg" width="200px" height="200px">

        <p>My Image 3</p>
        <img src="https://example.com/image-3.jpg" width="200px" height="200px">

        <p>My Image 4</p>
        <img src="https://example.com/image-4.jpg" width="200px" height="200px">

        <p>My Image 5</p>
        <img src="https://example.com/image-5.jpg" width="200px" height="200px">
    </body>
</html>
在页面加载

后,

结果HTML 应该是这样的

<html>
    <body>
        <h1>My Gallery Page</h1>
        <p>My Image 1</p>
        <img src="https://example.com/image-1.jpg" width="200px" height="200px">

        <p>My Image 2</p>
        <img src="https://example.com/image-2.jpg" width="200px" height="200px">

        <p>My Image 3</p>
        <img nopin="nopin" src="https://example.com/image-3.jpg" width="200px" height="200px">

        <p>My Image 4</p>
        <img src="https://example.com/image-4.jpg" width="200px" height="200px">

        <p>My Image 5</p>
        <img nopin="nopin" src="https://example.com/image-5.jpg" width="200px" height="200px">
    </body>
</html>

任何帮助都非常感谢,提前感谢。

8 个答案:

答案 0 :(得分:6)

您可以使用jQuery的属性选择器,然后使用attr()添加nopin属性来实现此目的。

值得注意的是nopin是一个非标准属性,可能会导致HTML有效性出现问题。如果可能的话,我建议改用data-nopin

&#13;
&#13;
jQuery(function($) {
  var img_src_1 = "https://example.com/image-3.jpg";
  var img_src_2 = "https://example.com/image-5.jpg";

  $(`img[src="${img_src_1}"], img[src="${img_src_2}"]`).attr('nopin', 'nopin');
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>My Gallery Page</h1>
<p>My Image 1</p>
<img src="https://example.com/image-1.jpg" width="200px" height="200px">

<p>My Image 2</p>
<img src="https://example.com/image-2.jpg" width="200px" height="200px">

<p>My Image 3</p>
<img src="https://example.com/image-3.jpg" width="200px" height="200px">

<p>My Image 4</p>
<img src="https://example.com/image-4.jpg" width="200px" height="200px">

<p>My Image 5</p>
<img src="https://example.com/image-5.jpg" width="200px" height="200px">
&#13;
&#13;
&#13;

答案 1 :(得分:3)

请尝试以下简单易用的解决方案:

        var img_src_1 = "https://example.com/image-3.jpg";
        var img_src_2 = "https://example.com/image-5.jpg";
        jQuery("img").each(function() {
        if(jQuery(this).attr("src") == img_src_1 || jQuery(this).attr("src") == img_src_2)
          {
            jQuery(this).attr("nopin", "nopin");
          }
       });

答案 2 :(得分:2)

使用此选择器img[src="'+img_src_1+'"]而非find

&#13;
&#13;
$(document).ready(function() {
  // find img tag by src value and add new attribute nopin="nopin" into this img tag
  var img_src_1 = "https://example.com/image-3.jpg";
  var img_src_2 = "https://example.com/image-5.jpg";

  // I want to find images with src url value is img_src_1 & img_src_2 and add new attribute nopin="nopin"
  var result1 = $('img[src="'+img_src_1+'"]');
  var result2 = $('img[src="'+img_src_2+'"]');
  result1.attr('nopin','nopin');
  result2.attr('nopin','nopin');
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>My Gallery Page</h1>
<p>My Image 1</p>
<img src="https://example.com/image-1.jpg" width="200px" height="200px">

<p>My Image 2</p>
<img src="https://example.com/image-2.jpg" width="200px" height="200px">

<p>My Image 3</p>
<img src="https://example.com/image-3.jpg" width="200px" height="200px">

<p>My Image 4</p>
<img src="https://example.com/image-4.jpg" width="200px" height="200px">

<p>My Image 5</p>
<img src="https://example.com/image-5.jpg" width="200px" height="200px">
&#13;
&#13;
&#13;

推荐版本:

无需使用完整网址,只需编写图片名称并通过此选择器img[src*="'+img_name+'"]找到它,然后使用data作为属性。

&#13;
&#13;
var img_src_1 = "image-3.jpg";
var img_src_2 = "image-5.jpg";

$('img[src*="' + img_src_1 + '"], img[src*="' + img_src_2 + '"]').attr('data-nopin', 'nopin');
&#13;
img[data-nopin] {
  border: 1px solid red;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>My Gallery Page</h1>
<p>My Image 1</p>
<img src="https://example.com/image-1.jpg" width="200px" height="200px">

<p>My Image 2</p>
<img src="https://example.com/image-2.jpg" width="200px" height="200px">

<p>My Image 3</p>
<img src="https://example.com/image-3.jpg" width="200px" height="200px">

<p>My Image 4</p>
<img src="https://example.com/image-4.jpg" width="200px" height="200px">

<p>My Image 5</p>
<img src="https://example.com/image-5.jpg" width="200px" height="200px">
&#13;
&#13;
&#13;

答案 3 :(得分:1)

尝试以下

var result1 = $('img[src="'+ img_src_1 + '"]');
var result2 = $('img[src="'+ img_src_2 + '"]');
result1.attr("nopin", "nopin");
result2.attr("nopin", "nopin");

供参考,jQuery Attribute Selector

答案 4 :(得分:1)

循环图像并使用attr查找图像的src。检查图像的src,如果条件匹配,则使用attr()函数向元素添加新属性。

jQuery(document).ready(function(){
            // find img tag by src value and add new attribute nopin="nopin" into this img tag
            var img_src_1 = "https://example.com/image-3.jpg";
            var img_src_2 = "https://example.com/image-5.jpg";

            // I want to find images with src url value is img_src_1 & img_src_2 and add new attribute nopin="nopin"
            var result1 = jQuery('img').find('src', img_src_1);
            var result2 = jQuery('img').find('src', img_src_2);
            $('img').each(function(){
              if($(this).attr('src') == img_src_1 || $(this).attr('src') == img_src_2) {
              $(this).attr('nopin', 'nopin');
              
              }
            });
        });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>My Gallery Page</h1>
        <p>My Image 1</p>
        <img src="https://example.com/image-1.jpg" width="200px" height="200px">

        <p>My Image 2</p>
        <img src="https://example.com/image-2.jpg" width="200px" height="200px">

        <p>My Image 3</p>
        <img src="https://example.com/image-3.jpg" width="200px" height="200px">

        <p>My Image 4</p>
        <img src="https://example.com/image-4.jpg" width="200px" height="200px">

        <p>My Image 5</p>
        <img src="https://example.com/image-5.jpg" width="200px" height="200px">

答案 5 :(得分:1)

您可以使用 vanilla-JS

/* retrieve images ($= means "attribute ending with") 
 * Note how easy is to look for several images (no need to write
 * ugly long and error-prone comparisons with ||), you just select them 
 * as you would do it in CSS 
 */ 
var imagesNoPin = document.querySelectorAll(
                 '[src $= "-3.jpg"], 
                  [src $= "-5.jpg"]');

/* set the attribute for each image. Note that you don't need to loop
 * over ALL images, as in other answers: you have already found them
 * before via the js-native querySelectorAll() method
 */
[].forEach.call(imagesNoPin, function(img) {
   img.setAttribute('nopin', 'nopin'); // with an attribute
   // img.dataset.nopin = "nopin"      // with a data-nopin attribute (suggested)
});

/* same result with spread [...] operator */
// [...imagesNoPin].map((img) => img.setAttribute('nopin', 'nopin'));
  

Codepen demo

(在示例中,带有nopin属性的图片已在CSS中使用outline进行装饰

答案 6 :(得分:1)

以下代码可以帮助您。

 jQuery(document).ready(function(){
            // find img tag by src value and add new attribute nopin="nopin" into this img tag
            var img_src_1 = "https://example.com/image-3.jpg";
            var img_src_2 = "https://example.com/image-5.jpg";

            // I want to find images with src url value is img_src_1 & img_src_2 and add new attribute nopin="nopin"
            jQuery('img[src="' + img_src_1 + '"]').attr("nopin" ,"nopin" );
            jQuery('img[src="' + img_src_2 + '"]').attr("nopin" ,"nopin" );

        });

答案 7 :(得分:1)

试试这个 - 用于内部代码的Javascript。实际上你也不需要Jquery(不必要地增加带宽开销)。

<html>
<head>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>

</head>
<body>
    <h1>My Gallery Page</h1>
    <p>My Image 1</p>
    <img src="https://www.drupal.org/files/issues/sample_7.png" width="200px" height="200px">

    <p>My Image 2</p>
    <img src="https://software-carpentry.org/files/2014/01/novice-sample.png" width="200px" height="200px">

    <p>My Image 3</p>
    <img src="https://software-carpentry.org/files/2014/01/novice-sample.png" width="200px" height="200px">

    <p>My Image 4</p>
    <img src="https://bellard.org/bpg/2.png" width="200px" height="200px">

    <p>My Image 5</p>
    <img src="https://bellard.org/bpg/2.png" width="200px" height="200px">
</body>
<script>
    jQuery(document).ready(function(){
        // find img tag by src value and add new attribute nopin="nopin" into this img tag
        var img_src_1 = "https://software-carpentry.org/files/2014/01/novice-sample.png";
        var img_src_2 = "https://www.drupal.org/files/issues/sample_7.png";

        // I want to find images with src url value is img_src_1 & img_src_2 and add new attribute nopin="nopin"
        var result = document.getElementsByTagName('img');
        for(var i=0;i<result.length;i++){
            if(result[i].src==img_src_1 || result[i].src==img_src_2){
                result[i].setAttribute("nopin", "nopin");
            }
        }
    });
    </script>