jQuery hasAttr 检查元素上是否有属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1318076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery hasAttr checking to see if there is an attribute on an element
提问by Mark
How do you check if there is an attribute on an element in jQuery? Similar to hasClass
, but with attr
?
你如何检查jQuery中的元素是否有属性?类似于hasClass
,但与attr
?
For example,
例如,
if ($(this).hasAttr("name")) {
// ...
}
回答by strager
var attr = $(this).attr('name');
// For some browsers, `attr` is undefined; for others,
// `attr` is false. Check for both.
if (typeof attr !== typeof undefined && attr !== false) {
// ...
}
回答by Domenic
How about just $(this).is("[name]")
?
刚刚$(this).is("[name]")
怎么样?
The [attr]
syntax is the CSS selector for an element with an attribute attr
, and .is()
checks if the element it is called on matches the given CSS selector.
该[attr]
语法是用于与属性的元素的CSS选择attr
,并.is()
检查是否元件它在比赛称为给定的CSS选择。
回答by karim79
If you will be checking the existence of attributes frequently, I would suggest creating a hasAttr
function, to use as you hypothesized in your question:
如果您要经常检查属性的存在,我建议创建一个hasAttr
函数,按照您在问题中的假设使用:
$.fn.hasAttr = function(name) {
return this.attr(name) !== undefined;
};
$(document).ready(function() {
if($('.edit').hasAttr('id')) {
alert('true');
} else {
alert('false');
}
});
<div class="edit" id="div_1">Test field</div>
回答by Tristan Warner-Smith
You're so close it's crazy.
你离得这么近简直是疯了。
if($(this).attr("name"))
There's no hasAttr but hitting an attribute by name will just return undefined if it doesn't exist.
没有 hasAttr 但按名称点击属性将返回 undefined 如果它不存在。
This is why the below works. If you remove the name attribute from #heading the second alert will fire.
这就是为什么下面的工作。如果您从 #heading 中删除 name 属性,则会触发第二个警报。
Update:As per the comments, the below will ONLYwork if the attribute is present ANDis set to something not if the attribute is there but empty
更新:按照意见,下面将仅如果属性是目前的工作和设置的东西不是如果属性是有,但空
<script type="text/javascript">
$(document).ready(function()
{
if ($("#heading").attr("name"))
alert('Look, this is showing because it\'s not undefined');
else
alert('This would be called if it were undefined or is there but empty');
});
</script>
<h1 id="heading" name="bob">Welcome!</h1>
回答by Domenic
回答by JamesM-SiteGen
The best way to do this would be with filter()
:
最好的方法是使用filter()
:
$("nav>ul>li>a").filter("[data-page-id]");
It would still be nice to have .hasAttr(), but as it doesn't exist there is this way.
拥有 .hasAttr() 仍然很好,但由于它不存在,所以有这种方式。
回答by rlemon
Object.prototype.hasAttr = function(attr) {
if(this.attr) {
var _attr = this.attr(attr);
} else {
var _attr = this.getAttribute(attr);
}
return (typeof _attr !== "undefined" && _attr !== false && _attr !== null);
};
I came a crossed this while writing my own function to do the same thing... I though I'd share in case someone else stumbles here. I added null because getAttribute() will return null if the attribute does not exist.
我在编写自己的函数来做同样的事情时遇到了这个问题......我虽然我会分享以防其他人在这里绊倒。我添加了 null 因为如果属性不存在 getAttribute() 将返回 null。
This method will allow you to check jQuery objects and regular javascript objects.
此方法将允许您检查 jQuery 对象和常规 javascript 对象。
回答by user398341
You can also use it with attributes such as disabled="disabled" on the form fields etc. like so:
您还可以将它与表单字段上的 disabled="disabled" 等属性一起使用,如下所示:
$("#change_password").click(function() {
var target = $(this).attr("rel");
if($("#" + target).attr("disabled")) {
$("#" + target).attr("disabled", false);
} else {
$("#" + target).attr("disabled", true);
}
});
The "rel" attribute stores the id of the target input field.
“rel”属性存储目标输入字段的 id。
回答by bmarti44
I wrote a hasAttr() plugin for jquery that will do all of this very simply, exactly as the OP has requested. More information here
我为 jquery 编写了一个 hasAttr() 插件,它将非常简单地完成所有这些,完全按照 OP 的要求。更多信息在这里
EDIT:My plugin was deleted in the great plugins.jquery.com database deletion disaster of 2010. You can look herefor some info on adding it yourself, and why it hasn't been added.
编辑:我的插件在 2010 年的 plugins.jquery.com 数据库删除灾难中被删除。您可以在此处查看有关自己添加它的一些信息,以及为什么没有添加它。