jQuery:如何检查元素属性是否包含字符串的一部分?

时间:2022-10-22 07:19:54

Like, I want to check if a link has a certain domain, or already has parameters attached to the end.

就像,我想检查链接是否具有某个域,或者已经在末尾附加了参数。

is there a way of doing a regex thing like ~= maybe?

有没有办法做一个正则表达式的事情~~也许?

currently, I have

目前,我有

    alert(ref);
        $j("a[href*='mysite']").each(function(i){
            alert("hello");
            $j(this).attr('href',$j(this).attr('href') + "?ref=" + $j.cookie.get("tb_ref"));
        }); 

but the selector isn't working. (I never see the Hello alert, but I seed the alert ref alert.

但选择器不工作。 (我从未看到过Hello警报,但我播种了警报引用警报。

my a tags

我的标签

<a href="http://domain.mysite.com/">yeah, link</a>
<a href="http://google.com/">g, link</a>

3 个解决方案

#1


14  

Use the substring/contains selector:

使用substring / contains选择器:

$("selector[name*='mystring']")

$( “选择器[名称* = 'MyString的']”)

finds attribute name="somemystring"

找到属性name =“somemystring”

If you're looking for the whole word, delimited by spaces, then use the whole word selector:

如果你正在寻找由空格分隔的整个单词,那么使用整个单词选择器:

$("selector[name~='mystring']")

$( “选择器[名〜= 'MyString的']”)

finds attribute name="some mystring is good"

找到属性名称=“有些mystring很好”

EDIT I took your code from above and created a jsFiddle for it. I see the one 'Hello' alert pop-up. (I didn't do an alert for the alert(ref), as I didn't know what ref was.) So the selector works for your href attributes. You're alising jQuery different than normal (normal = $, you're using $j). Did you register this alias properly? If you use $ or jQuery instead, does your code work then?

编辑我从上面拿了你的代码并为它创建了一个jsFiddle。我看到一个'Hello'警告弹出窗口。 (我没有对alert(ref)做警报,因为我不知道ref是什么。)因此选择器适用于你的href属性。你正在使jQuery与正常情况不同(正常= $,你使用的是$ j)。你是否正确注册了这个别名?如果您使用$或jQuery代替,那么您的代码是否正常工作?

#2


2  

You can use the attribute contains selector (*=), like this:

您可以使用属性contains selector(* =),如下所示:

$("a[href*=somedomain.com]")

There are other attribute selectors available as well.

还有其他属性选择器可用。

#3


1  

If the link is <a id='link' href='www.google.com/myparam'></a> and you want to tell if the link contains myparam use jQuery/javascript:

如果该链接是 ,并且您想要告诉该链接是否包含myparam,请使用jQuery / javascript:

/myparam/.test($("#link").attr('href'))

#1


14  

Use the substring/contains selector:

使用substring / contains选择器:

$("selector[name*='mystring']")

$( “选择器[名称* = 'MyString的']”)

finds attribute name="somemystring"

找到属性name =“somemystring”

If you're looking for the whole word, delimited by spaces, then use the whole word selector:

如果你正在寻找由空格分隔的整个单词,那么使用整个单词选择器:

$("selector[name~='mystring']")

$( “选择器[名〜= 'MyString的']”)

finds attribute name="some mystring is good"

找到属性名称=“有些mystring很好”

EDIT I took your code from above and created a jsFiddle for it. I see the one 'Hello' alert pop-up. (I didn't do an alert for the alert(ref), as I didn't know what ref was.) So the selector works for your href attributes. You're alising jQuery different than normal (normal = $, you're using $j). Did you register this alias properly? If you use $ or jQuery instead, does your code work then?

编辑我从上面拿了你的代码并为它创建了一个jsFiddle。我看到一个'Hello'警告弹出窗口。 (我没有对alert(ref)做警报,因为我不知道ref是什么。)因此选择器适用于你的href属性。你正在使jQuery与正常情况不同(正常= $,你使用的是$ j)。你是否正确注册了这个别名?如果您使用$或jQuery代替,那么您的代码是否正常工作?

#2


2  

You can use the attribute contains selector (*=), like this:

您可以使用属性contains selector(* =),如下所示:

$("a[href*=somedomain.com]")

There are other attribute selectors available as well.

还有其他属性选择器可用。

#3


1  

If the link is <a id='link' href='www.google.com/myparam'></a> and you want to tell if the link contains myparam use jQuery/javascript:

如果该链接是 ,并且您想要告诉该链接是否包含myparam,请使用jQuery / javascript:

/myparam/.test($("#link").attr('href'))