搜索属性值时,是否在jQuery中使用引号? [英] Do you use quotes in jQuery when searching for attribute values?

查看:68
本文介绍了搜索属性值时,是否在jQuery中使用引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的链接:

<a data-btn="login"></a>

如果我希望使用data属性在jQuery中进行选择?

If I wished to select in jQuery using the data attribute which should I do?

var a = $("[data-btn='login']");  //This?

var a = $("[data-btn=login]");    //Or this?

推荐答案

从jQuery 1.5开始,这两种方法都将起作用,并且功能相同.

As of jQuery 1.5, both approaches will work, and function identically.

jQuery过去常常要求在属性值两边加上引号,因此您的第二个选择器将不适用于以前的版本.如果由于某种原因而使用旧版本,请使用第一个选择器.

jQuery used to require quotes around attribute values, so your second selector won't work with previous versions. If you're stuck with an older version for some reason, use the first selector.

这篇关于搜索属性值时,是否在jQuery中使用引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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