I have seen loc = $('<a>', {href:window.location})[0];
appearing in alot of samples and they all seem to take it for granted. I am not new to jQuery, but only recently decided to start looking after my URLs, so my question is what does the weird combination of syntaxes mean?
Would appreciate if you could provide a usage example, or a slightly different reference that refers to the same thing that the loc
here does.
Thank you!
答案 0 :(得分:3)
$('<a>', {href:window.location})
创建一个jquery对象,此处使用[0]
来访问底层DOM元素。您还可以使用get(index)
来访问底层DOM元素。
loc = $('<a>', {href:window.location});
console.log(loc[0]);
console.log(loc);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
答案 1 :(得分:1)
代码loc = $('<a>', {href:window.location})
为您提供了jquery版本对象,[0]
为您提供了由它创建的DOM节点,没有额外的选项。
var loc = $('<a>', {href:window.location});
console.log(loc);
console.log(loc[0]);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
答案 2 :(得分:0)
此代码创建新的a
元素,其href属性等于当前window.location
,并将其分配给loc
变量。