jQuery:如果此HREF包含 [英] jQuery: If this HREF contains

查看:75
本文介绍了jQuery:如果此HREF包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能使它正常工作?

Why can't I get this to work??

$("a").each(function() {
    if ($(this[href$="?"]).length()) {
        alert("Contains questionmark");
    }
});

Ps .:这只是一个简化的示例,以使您更容易获得概述.

Ps.: This is just at simplifyed example, to make it easier for you to get an overview.

推荐答案

您可以直接选择感兴趣的元素.

You could just outright select the elements of interest.

$('a[href*="?"]').each(function() {
    alert('Contains question mark');
});

http://jsfiddle.net/mattball/TzUN3/

请注意,您使用的是上面的 attribute-ends-with选择器代码使用 attribute-contains选择器,这听起来像是您实际上瞄准.

Note that you were using the attribute-ends-with selector, the above code uses the attribute-contains selector, which is what it sounds like you're actually aiming for.

这篇关于jQuery:如果此HREF包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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