我在学习jQuery,当使用:eq()
选择器时,我得到这个'undefined'错误。下面是HTML::eq()选择器返回undefined
<!DOCTYPE html>
<html>
<head>
<title>:eq() selector example</title>
</head>
<body>
<div>
<button>Button #1</button>
<button>Button #2</button>
<button>Button #3</button>
<button>Button #4</button>
<button>Button #5</button>
<button>Button #6</button>
</div>
<script src="jquery-2.1.4.min.js"></script>
<script src="script.js"></script>
</body>
</html>
和脚本:
var myButton = $('button:eq(5)');
console.log(myButton.textContent);
当我使用var myButton = $('button')[5];
一切都很好,虽然。
需要使用'文本()'有 –
':当量()'和'.EQ()'都得到jQuery对象,而'[]'产生DOM节点。在你的例子中,'button:eq(5)'不返回undefined,它返回没有'textContent'的东西。 –