【问题标题】:Change the Image Link to "#" using jQuery使用 jQuery 将图像链接更改为“#”
【发布时间】:2013-08-18 18:07:52
【问题描述】:

我验证图像路径是图像路径还是其他路径。如果是图像,则将特定的 href 值替换为 # 但 # 值会影响所有链接。如何将图片链接替换为#

jQuery(document).ready(function($) {
   $("a").each(function(i, el) {
      var href_value = el.href;
      if (/\.(jpg|png|gif)$/.test(href_value)) {
         jQuery('a').prop('href', '#');
      }
   });
});

这里是

FIDDLE

任何建议都会很棒。

【问题讨论】:

  • jQuery(el).prop('href', '#'); 而不是 jQuery('a').prop('href', '#');

标签: javascript jquery image href prop


【解决方案1】:

使用这个

jQuery(this).prop('href', '#');

jQuery(el).prop('href', '#');

【讨论】:

    【解决方案2】:

    像这样尝试this

     jQuery(document).ready(function($) {
           $("a").each(function() {
                var href_value = $(this).attr('href');
                if (/\.(jpg|png|gif)$/.test(href_value)) {
                   jQuery(this).prop('href', '#');
                } 
            });
      });
    

    【讨论】:

      【解决方案3】:

      所有链接都将其 href 设置为 # 的原因是您使用 jQuery('a').prop('href', '#');当您找到图片链接时。 jQuery('a') 查找整个页面中的所有 a-tags。

      尝试使用您的 el 变量。比如:

      if (/\.(jpg|png|gif)$/.test(href_value)) {
         jQuery(el).prop('href', '#');
      }
      

      【讨论】:

        【解决方案4】:

        试试下面的代码

        el.href="#";
        

        http://jsfiddle.net/Q3Zwh/3/

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-11-26
          • 1970-01-01
          • 2013-10-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-01
          • 1970-01-01
          相关资源
          最近更新 更多