我不知道如何使用我的数组包含的名称对所有输入调用我的函数。我试过了:
var names = ["name1", "name2"];
$(document).ready(function(){
$('input[name=names[0]').on("mouseenter", function() {
$(this).attr('title', 'This is the hover-over text');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>Head</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<input type="text" name="name1"> <!--This should have title-->
<input type="text" name="name2"> <!--This should have title-->
<input type="text" name="name3"> <!--This should not have title--
我试着把第一个元素放在那里,但我什至做不到。我是 jquery(也是 js)的初学者,所以解决方案可能很明显。
最佳答案
您只需在数组上运行一个循环并为每个包含输入名称的数组元素添加事件监听器。
$(document).ready(function(){
$.each(names , function(index, item) {
$("input[name="+ item +"]").on("mouseenter", function() {
$(this).attr('title', 'This is the hover-over text');
});
});
});
关于javascript - 使用 jquery 将 mouseenter 事件添加到数组中具有名称的所有输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52762350/