javascript 替代 ruby .none?
javascript alternative to ruby .none?
如果none满足条件,我如何迭代do something
。对于 Ruby 中的上下文,我可以执行 some_array.none?{|t| t == "something"}
并返回一个布尔值。
在 js 中,我尝试使用 [href] 属性迭代所有元素,并在 none 具有 class "active"
时执行某些操作
$("[href]").each(function() {
if (this.href == window.location.href) {
$(this).addClass("active");
}
});
我执行上述操作来设置我的活动链接的样式,但我希望我的其中一个链接设置为活动链接(如果其他 none 链接是活动链接)。我是 js 的新手,所以我确定可能有更好的方法。
好的,所以我完成了我想要的,但它有点老套和丑陋,所以仍然对替代品持开放态度,这就是我所做的
var counter = 0;
$("[href]").each(function() {
if (this.href == window.location.href) {
counter = +1;
$(this).addClass("active");
}
});
if (counter === 0) {
$("[href='/snippets/new']").addClass("active");
}
不能把选择器条件移到jQuery选择器中,然后测试选择器长度吗?
var selector = $("[href=" + window.location.href + "]");
selector.each(function() {
$(this).addClass("active");
});
if(selector.length == 0) {
$("[href='/snippets/new/]").addClass("active");
}
如果none满足条件,我如何迭代do something
。对于 Ruby 中的上下文,我可以执行 some_array.none?{|t| t == "something"}
并返回一个布尔值。
在 js 中,我尝试使用 [href] 属性迭代所有元素,并在 none 具有 class "active"
时执行某些操作$("[href]").each(function() {
if (this.href == window.location.href) {
$(this).addClass("active");
}
});
我执行上述操作来设置我的活动链接的样式,但我希望我的其中一个链接设置为活动链接(如果其他 none 链接是活动链接)。我是 js 的新手,所以我确定可能有更好的方法。
好的,所以我完成了我想要的,但它有点老套和丑陋,所以仍然对替代品持开放态度,这就是我所做的
var counter = 0;
$("[href]").each(function() {
if (this.href == window.location.href) {
counter = +1;
$(this).addClass("active");
}
});
if (counter === 0) {
$("[href='/snippets/new']").addClass("active");
}
不能把选择器条件移到jQuery选择器中,然后测试选择器长度吗?
var selector = $("[href=" + window.location.href + "]");
selector.each(function() {
$(this).addClass("active");
});
if(selector.length == 0) {
$("[href='/snippets/new/]").addClass("active");
}