jQuery 如何在javascript中获取html标签值(div)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19451144/
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-27 00:00:14  来源:igfitidea点击:

How do I get html tag value (div) in javascript

javascriptjqueryhtml

提问by Angwenyi

I have implemented this function (libphonenumber javascript )in a website http://www.phoneformat.com/

我已经在网站http://www.phoneformat.com/ 中实现了这个功能(libphonenumber javascript)

How do i get the value returned by this html tag. Whether Yes or No

我如何获得这个 html 标签返回的值。是或否

< DIV id="phone_valid" class="popup-value"></DIV>' 

I have tried this

我试过这个

function checkSubmit(){
var country=$("#phone_valid").val();
if(country=="No")
{
    alert("Not a valid number");
    return false;
}

So far no luck

到目前为止没有运气

回答by Gurpreet Singh

The .val()method is primarily used to get the values of form elements such as input, selectand textarea.

.val()方法主要用于获取input,select和等表单元素的值textarea

Use

$("#phone_valid").text();

to get DIV text content or

获取 DIV 文本内容或

$("#phone_valid").html();

if you want markup.

如果你想要标记。

回答by sacha barber

You probably want to do this:

你可能想要这样做:

$("#phone_valid").html();

or

或者

$("#phone_valid").text();

回答by Tim S

.val() is for form elements. You should use .text() or .html() to get the value from a DIV.

.val() 用于表单元素。您应该使用 .text() 或 .html() 从DIV.

HTML

HTML

<DIV id="phone_valid" class="popup-value"></DIV>

JavaScript

JavaScript

function checkSubmit(){
    var country=$("#phone_valid").html();
    if(country=="No")
    {
        alert("Not a valid number");
        return false;
    }
}

Hope this helps!

希望这可以帮助!

回答by Snake Eyes

First, no space between <and div(saw here: < DIV id="phone_valid" class="popup-value"></DIV>')

首先,之间没有空格<div(在这里看到:< DIV id="phone_valid" class="popup-value"></DIV>'

Second:

第二:

function checkSubmit(){
var country=$("#phone_valid").text(); // it is a div not input to get val().
if(country=="No")
{
    alert("Not a valid number");
    return false;
}

回答by andlrc

In vanilla JavaScript you can use document.getElementByIdto get a specific node using ID:

在 vanilla JavaScript 中,您可以使用document.getElementByIdID 获取特定节点:

var node = document.getElementById('phone_valid');

And then to get the text from that node you will need to use:

然后要从该节点获取文本,您需要使用:

var text = node.innerText || node.textContent;

The jQuery .val()method is used on form fields like input, textarea, select...

jQuery.val()方法用于表单字段,例如input, textarea, select...

回答by RobotMan

you can use this:

你可以使用这个:

document.getElementById("phone_valid").innerHTML;

回答by Stphane

If you need to include HTML comments then consider using contents() method

如果您需要包含 HTML 注释,请考虑使用 contents() 方法

$('#mydiv').contents()

$('#mydiv').contents()

Other wise html()method or even text()will be what you are looking for because val()purpose is for form elements ;)

其他明智的html()方法甚至text()将是您正在寻找的方法,因为val()目的是用于表单元素;)