我有一个id为'item-1'
的锚标记 <a id='item-1' href='#something'>
我希望将其href
属性存储在名为'href。'的新变量中。
如何使用jquery完成此操作?
答案 0 :(得分:0)
您可以使用 attr()
var href = $('#item-1').attr('href');
console.log(href);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a href="test" id="item-1"></a>
&#13;
或在JavaScript中使用 getAttribute()
var href = document.getElementById('item-1').getAttribute('href');
console.log(href);
&#13;
<a href="test" id="item-1"></a>
&#13;