本文翻译自:jQuery hasAttr checking to see if there is an attribute on an element [duplicate]

Possible Duplicate: 可能重复:
Check existence of an attribute with JQuery 使用JQuery检查属性是否存在

How do you check if there is an attribute on an element in jQuery? 你如何检查jQuery中的元素是否有属性? Similar to hasClass , but with attr ? hasClass类似,但是attr

For example, 例如,

if ($(this).hasAttr("name")) {// ...
}

#1楼

参考:https://stackoom.com/question/5WtI/jQuery-hasAttr检查元素是否有属性-重复


#2楼

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 ONLY work if the attribute is present AND is 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');elsealert('This would be called if it were undefined or is there but empty');
});
</script>
<h1 id="heading" name="bob">Welcome!</h1>

#3楼

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) {// ...
}

#4楼

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>

#5楼

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()仍然会很好,但因为它不存在就有这种方式。


#6楼

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。

jQuery hasAttr检查元素是否有属性[重复]相关推荐

  1. 用jQuery访问指定元素的父元素

    Target the Parent of an Element Using jQuery 每个HTML元素根据继承属性都有父parent元素. 举个例子,h3 元素的父元素是 <div clas ...

  2. jQuery(一):概述、选择器、操作(元素本身、属性、内容、样式)、元素遍历、事件

    目录 一.jQuery概述 1.1 什么是jQuery 1.2 jQuery的优势 1.3 jQuery版本支持 1.4 jQuery引入 1.5 jQuery核心用法 1.5.1 $介绍 1.5.2 ...

  3. 怎样设置html的元素属性,Jquery怎么设置元素的属性和样式?

    Jquery怎么设置元素的属性和样式?下面本篇文章给大家介绍一下使用jQuery操作元素属性与样式的方法.有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助. 元素属性和Dom属性 对于下 ...

  4. Java程序员从笨鸟到菜鸟之(八十八)跟我学jquery(四)JQuery框架操作元素的属性与样式

    在前面几篇博客中,我们初步了解了一下jQuery的好处,基本语法,还有一些基本函数,这是学习jquery的基础,在这篇博客中,我们一起来学习一下JQuery框架操作元素的属性与样式,在web开发中,修 ...

  5. jq追加html属性,jQuery 操作 HTML 元素和属性的方法

    jQuery拥有操作 HTML 元素和属性的强大方法. 1. 获取HTML 元素的内容和属性 (1) 获得内容:  text().html() 以及 val()方法 My Test JQuery $( ...

  6. 用JQuery操作元素的style属性

    可以直接利用css()方法获取元素的样式属性,JQuery代码如下: 1 $("p").css("color");  //获取p元素的样式颜色 无论color属 ...

  7. jQuery 学习-DOM篇(一):jQuery 创建元素并添加属性

    推荐阅读 Helm3(K8S 资源对象管理工具)视频教程:https://edu.csdn.net/course/detail/32506 Helm3(K8S 资源对象管理工具)博客专栏:https: ...

  8. JavaScript(23) 创建元素标签和属性在body中(jQuery插件)

    效果图: 代码:(注意引入jQuery插件!!) <!DOCTYPE html> <html><head lang="en"><meta ...

  9. html添加删除元素属性,jquery怎么删除元素的属性?

    jquery怎么删除元素的属性?下面本篇文章给大家介绍一下.有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助. 在jquery中,可以使用removeAttr()方法来删除元素的属性.r ...

最新文章

  1. matlab正态分布
  2. Web API 2 入门——创建ASP.NET Web API的帮助页面(谷歌翻译)
  3. datapump跨平台升级迁移的总结
  4. 孙正义重申:计划在五年内将芯片设计公司Arm重新上市
  5. 数据库性能监控之联合索引使用
  6. mysql实例备份和单库备份_史上最简单的MySQL数据备份与还原教程(上)(三十五)...
  7. ipfs add命令
  8. 数据链路层协议(1)
  9. 展览会议签到方式之——CES亚洲消费电子展自助签到
  10. BZOJ-1076: [SCOI2008]奖励关 (概率期望DP 未完待续)
  11. openGauss 极简版安装
  12. python生成渐变颜色数组
  13. 1.1 编辑楼层标高
  14. UWA DAY 2021 议程亮相
  15. 谭浩强《C语言程序设计 》习题 6-3
  16. esp-12s WiFi模块连接 stm32f4单片机与电脑数据传输
  17. linux之创建守护进程
  18. linux查看数据库密码命令
  19. MXone Pro自适应2.0影视模板西瓜视频主题苹果cmsV10模板
  20. 36个Excel实用技巧

热门文章

  1. centos7下安装oracle11gR2
  2. 修改Windows登陆时显示上一次登陆的用户名
  3. MapX历史轨迹回放[开发源代码]:
  4. asp.net core3.0 mvc 用 autofac
  5. JS——构造函数、原型与实例之间的关系 及 原型链 的描述
  6. C语言学习笔记--数组参数和指针参数
  7. 提示gtk错误,无法打开便器器(sudo gedit filename失败)
  8. ACM - ICPC World Finals 2013 A Self-Assembly
  9. 判断客户端是否安装FlashPlayer及版本
  10. delphi连接SQL2005做的数据库管理系统的一些部署问题