【发布时间】:2018-02-22 17:58:49
【问题描述】:
我的问题很简单。是否可以为每个选择器设置不同的值?在我真实的项目代码中,我是这样操作的。
$("#zero").on("click", function() {
pressNumButton('0');
});
$("#one").on("click", function() {
pressNumButton('1');
});
是的,我知道这个问题的一种解决方案。我可以为用户按下的每个数字使用相同的类,然后获取它的值,因为每个 id #zero - #nine 的值都是 0 - 9。是否可以使用 ID 来实现?
在这里使用类是最好的解决方案吗?
我已经在我的 sn-p 中注释掉了一些行,以便您更好地理解它。
// It should display One for first selector, and Two for the second one
//$('#one, #two').text('One');
// In other words, how can I do this job with a one-liner? I know I can do it with multiple lines but the problem I have many selectors, not just two. It's just an example
$('#one').text('One');
$('#two').text('Two');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="one"></div>
<div id="two"></div>
【问题讨论】:
标签: javascript jquery html jquery-selectors