使用jQuery将href属性存储为新变量

时间:2015-10-01 05:04:45

标签: jquery html5 frontend

我有一个id为'item-1'

的锚标记

<a id='item-1' href='#something'>

我希望将其href属性存储在名为'href。'的新变量中。

如何使用jquery完成此操作?

1 个答案:

答案 0 :(得分:0)

您可以使用 attr()

&#13;
&#13;
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;
&#13;
&#13;

或在JavaScript中使用 getAttribute()

&#13;
&#13;
var href = document.getElementById('item-1').getAttribute('href');

console.log(href);
&#13;
<a href="test" id="item-1"></a>
&#13;
&#13;
&#13;