jQuery-查找包含值的href并返回完整值 [英] jQuery - Find href that contains a value and return the full value

查看:89
本文介绍了jQuery-查找包含值的href并返回完整值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在正文中搜索包含/test/的href.然后,我想返回整个href值,例如/test/123/.

I'd like to search the body for an href that contains /test/. I then want to return the entire href value e.g. /test/123/.

以下是我到目前为止提出的内容.我知道它可以找到/test/,但是我在努力返回完整的href值.有什么想法吗?

Below is what I've come up with so far. I know that it can find /test/ but I'm struggling with returning the full href value. Any ideas?

function() {
    var htmlString = $('body').html().toString();
    var index = htmlString.indexOf("/test/");
    if (index != -1)
        return index.closest('div').find('a').attr('href');
}

HTML:

<div>
    <a href="/test/123/">Test</a>
</div>

谢谢.

推荐答案

您可以使用jQuery

You can do it like following using jQuery contain attribute selector.

$('a[href*="/test/"]').attr('href');

这篇关于jQuery-查找包含值的href并返回完整值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆