【问题标题】:How to find the href path of the anchor tag using jquery?如何使用jquery找到锚标签的href路径?
【发布时间】:2013-07-09 09:05:35
【问题描述】:

如何使用 jquery 获取并检查 href 路径是否包含图像路径或其他链接。 例如:

<a href="image.png"><img src="image.png"/></a>

在上面的示例中,锚文本包含图像 src,那么只有 jquery 函数才能找到 href 路径。即,仅当锚文本包含 img 标记时才获取 href 路径。

我查看了 jquery api 中的 .has 以检查图像。但是我如何检查href是否包含图像路径或其他路径。 我将根据 SO 用户的建议做一些事情,比如。

<script type="text/javascript">
                            jQuery(document).ready(function($) {
                                if (jQuery('a').has("img")) {  // it return the no. of img tag within a
                                    jQuery('a').has("img").prop('href', '#');
                                }

                            });
                        </script>

请参阅上面的脚本执行检查锚文本是否包含图像标签的操作,然后仅在锚标签的 href 链接上使用 #。 通过同样的方式添加喜欢做上面的脚本来检查href是否包含任何图像路径或链接。如果图像路径包含在 href 上,那么上面的脚本应该执行,否则它不会。

@Downvoters 的任何可能原因?

是否可以在 jquery 中做,任何建议将不胜感激。

【问题讨论】:

  • 您可以使用正则表达式测试 href 的扩展。前/\.(png|gif/jpg|jpeg)$/i.test(href)
  • 你可以在 jquery 中使用attr

标签: javascript jquery html jquery-ui


【解决方案1】:

你可以的

$('a:has(img)').click(function(){
    var href = this.href; //or $(this).attr('href')
    if(/\.(png|gif|jpg|jpeg)$/i.test(href)){
        alert('do')
    }
})

【讨论】:

    【解决方案2】:

    首先使用attr() jQuery函数找到href

    var href = $link.attr('href');
    

    然后您必须使用this question 的答案中的指示来验证href 是否是图像。

    【讨论】:

      【解决方案3】:

      使用attr - documentation here.

      例子:

      我的链接

      console.log($('#myLink').attr("href"));
      

      【讨论】:

        【解决方案4】:
        var href = $('a').attr('href');
        

        var href = $('a').prop('href');
        

        【讨论】:

          【解决方案5】:

          首先您必须选择链接 例如

          <a href="link" id="ourlink" ></a>
          

          然后就可以使用attr方法获取或设置href了

          $("#ourlink").attr('href'); // Here you get it
          $("#ourlink").attr('href','anotherlink'); //here you set it
          

          【讨论】:

            【解决方案6】:

            做这样的事情-

            var url = $('a').attr('href');
            
            if(verify url is of type image here){
            //true
            
            }
            else{
            //false
            
            }
            

            【讨论】:

              【解决方案7】:

              使用正则表达式检查 href 属性中的字符串:

              var patt=/(png|jpg|gif)/g;
              if(patt.test($('a').attr('href'))){
              //do something if image
              } else {
              //do something else
              }
              

              【讨论】:

                【解决方案8】:

                您可以测试锚标记的属性并使用正则表达式检查href是否具有带有图像文件扩展名的文件。

                        //Get the value of href attribute of anchor tag
                        var anchorHref = $("a:has(img)").attr("href");
                
                        //Check the extention of the file
                        if((/\.(gif|jpg|jpeg|tiff|png|bmp)$/i).test(anchorHref))
                        {
                            alert("It is Image Path"); 
                        }
                

                【讨论】:

                  猜你喜欢
                  • 2016-11-12
                  • 2023-03-31
                  • 2019-03-07
                  • 2019-05-30
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2010-11-10
                  相关资源
                  最近更新 更多