简体   繁体   English

如何获取href属性的值

[英]How to get the value of href attribute

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我想得到#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.恢复后,我想检查具有该 id 的模式是否已加载到 DOM 中。

you can add an ID for that element and call the attr() to get the attribute of href use the this:你可以为该元素添加一个 ID 并调用attr()来获取 href 的属性,使用这个:

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

在此处输入图片说明

To get the value of an attribute use attr() :要获取属性的值,请使用attr()

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

See example:见示例:

 $(document).ready(function () { console.log($('.nextinst').attr('href')); });
 <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM