为什么我无法让这个工作??
$("a").each(function() {
if ($(this[href$="?"]).length()) {
alert("Contains questionmark");
}
});
顺便提一句:这只是一个简化的例子,让你更容易地了解它。
为什么我无法让这个工作??
$("a").each(function() {
if ($(this[href$="?"]).length()) {
alert("Contains questionmark");
}
});
顺便提一句:这只是一个简化的例子,让你更容易地了解它。
$('a[href*="?"]').each(function() {
alert('Contains question mark');
});
http://jsfiddle.net/mattball/TzUN3/
注意,你使用的是attribute-ends-with
选择器,而上面的代码使用的是attribute-contains
选择器,这正是你实际想要的。?
在正则表达式中可能具有特殊含义。请查看 http://jsfiddle.net/mattball/GYDQc/。 - Matt Ball$("a").each(function() {
if (this.href.indexOf('?') != -1) {
alert("Contains questionmark");
}
});
this.href
而不使用不必要的jQuery会更简单。这也不完全正确,因为原始问题似乎想要找到"href"值的结尾处的"?"(这是可以理解的)。 - Pointy使用这个
$("a").each(function () {
var href=$(this).prop('href');
if (href.indexOf('?') > -1) {
alert("Contains questionmark");
}
});
它无法正常工作,因为语法上没有意义。在JavaScript中不能这样做。
但是,您可以使用jQuery:
if ($(this).is('[href$=?]'))
if (/\?$/.test(this.href))
试试这个:
$("a").each(function() {
if ($('[href$="?"]', this).length()) {
alert("Contains questionmark");
}
});
<a>
标签。 - Pointy