jQuery 如何使用jQuery从锚标记中获取值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5180306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:41:47  来源:igfitidea点击:

How to get the value from anchor tag using jQuery?

jquery

提问by axhixh

<a href="" id="a1">myval</a>

How to get the value which is in between anchor tag ie; here "myval" and then specify that value to a hidden box.I tried do it with my piece of code but couldn't figure it out.pls help

如何获取锚标记之间的值,即;这里是“myval”,然后将该值指定给一个隐藏的框。我尝试用我的一段代码来做,但无法弄清楚。请帮忙

<script type="text/javascript">
    $(document).ready(function(){
        $("#a1").click(function(e){
            //var str=$("#a1").getVal();        
            alert('hello');
        }
    });
</script>

回答by kieran

var a1_text = $('#a1').text();

回答by Ujjwal Manandhar

var a_href = $("#a1").attr("href");

var a_href = $("#a1").attr("href");

回答by rahul

You can use .text()to get the innertext of the anchor tag and .val()to store the value to a textbox.

您可以使用.text()获取锚标记.val()的内部文本并将值存储到文本框。

$(function(){
    $("#a1").click(function(){
        $("#yourtextbox").val($(this).text());
    });
});

回答by Prakash

Try this :

尝试这个 :

$('#a1').click(function(){
   $(this).text();
})

回答by Sikander

 $("#a1").click(function(e){
           var str=$("#a1").html();        
            alert(str);
 });

回答by Krish

<a href="" id="a1">myval</a>

you can use var yourValue= $('#a1').text();it will works fine

你可以使用 var yourValue= $('#a1').text();它会很好用

回答by Lightness Races in Orbit

"myVal" is not the link element's "value". It is the text that forms a child element inside it.

“myVal”不是链接元素的“值”。它是在其中形成子元素的文本。

Use:

用:

$(function(){
    $('#a1').click(function () {
        alert($(this).text());
    });
});