[name]
选择所有具有指定属性的元素,该属性可以是任何值。
jQuery("[attribute]")
attribute:一个属性名
例子
绑定一个单一的点击,增加了div的id的文本。
<!DOCTYPE html> <html> <head> <script src="https://www.lanmper.cn/static/js/jquery-3.5.0.js"></script> </head> <body> <div>no id</div> <div id="hey">with id</div> <div id="there">has an id</div> <div>nope</div> <script> $('div[id]').one('click', function(){ var idString = $(this).text() + ' = ' + $(this).attr('id'); $(this).text(idString); }); </script> </body> </html>