我有Chrome和Safari瀏覽器的問題:奇怪的行爲,jQuery的
代碼示例:
// Get all links with the same data-test attribute
links = $('[data-test]');
//The issue is here:
// This only works in Chrome, not in safari.
var test = links[0].testProperty;
// This works in Chrome and Safari.
var test2 = $(links[0]).attr('testProperty');
console.log(test)
console.log(test2)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" testProperty="valueTest1" data-test>Test1</a>
<a href="#" testProperty="valueTest2" data-test>Test2</a>
<a href="#" testProperty="valueTest3" data-test>Test3</a>
爲什麼Safari瀏覽器不允許links[0].testProperty
?
第一個也不適用於鉻... – DaniP
http://stackoverflow.com/questions/15010981/javascript-getting-custom-attribute-works-only-with-element-getattributeattrib – DaniP
'testProperty '是屬性,而不是屬性 – LGSon