How to get the value of href attribute

时间:2016-09-10 15:23:18

标签: javascript jquery html

From source code:

<a class="nextinst" data-dismiss="modal" data-toggle="modal" href="#401">
    <span class="glyphicon glyphicon-chevron-right"></span>
</a>

I want to get #401

I have unsuccessfully tried

$('.nextinst').attr('href').val()  
$('.nextinst').attr('href').html()
$('.nextinst').attr('href').text()

After I recover it, I then want to check if the modal with that id has been loaded in the DOM.

2 个答案:

答案 0 :(得分:1)

你可以为该元素添加一个ID并调用attr()来获取href的属性使用它:

  function clickIt(){
    var val = $("#linkHref").attr("href");
    alert(val);
  }

enter image description here

答案 1 :(得分:0)

要获取属性的值,请使用attr()

$('.nextinst').attr('href');

参见示例:

&#13;
&#13;
$(document).ready(function () {
  console.log($('.nextinst').attr('href'));
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="nextinst" data-dismiss="modal" data-toggle="modal" href="#401">
   <span class="glyphicon glyphicon-chevron-right"></span>
</a>
&#13;
&#13;
&#13;