简体   繁体   English

Javascript如果两个标签之间的文本声明

[英]Javascript If Statement for Text between two Tags

I am trying to write a javascript function with an "if" statement that will execute a command if the text between two tags match. 我正在尝试使用“if”语句编写一个javascript函数,如果两个标记之间的文本匹配,它将执行命令。 I have been able to write many "if" statements in which a command is executed when the value within an input textbox equals that of the "if" statement criteria such as: 我已经能够编写许多“if”语句,当输入文本框中的值等于“if”语句条件的值时,执行命令,例如:

function Test()
{
    if (document.getElementById('A').value == 1)
    {
      alert('test');
    }
}

<input type="button" id="B" onCLick="Test()"/>
<input type="text" id="A"/>

When I enter "1" in the textbox and press the button, an alert box will appeare. 当我在文本框中输入“1”并按下按钮时,将出现一个警告框。 The problem that I am having is that I would like to do the same with text bewteen two tags. 我遇到的问题是我想对两个标签的文本做同样的事情。 For example: 例如:

function Test()
{
        if (document.getElementById('A').value == 1)
    {
      alert('test');
    }
}

<input type="button" id="B" onCLick="Test()"/>
<p id="A">1</p>

In my project I would be using words instead of numbers, so I understand that I would have to surround the word in quotes. 在我的项目中,我将使用单词而不是数字,所以我理解我必须用引号括起来。 Unfortunately, this doesn't work. 不幸的是,这不起作用。 Is it possible to write an "if" statement like the one above that determines if text between two tags is true? 是否可以编写一个像上面那样的“if”语句来确定两个标签之间的文本是否为真? Also, I have tried document.getElementById('A').text, and document.getElementById('A').innerHTML, but none of those made a difference. 另外,我尝试过document.getElementById('A')。text和document.getElementById('A')。innerHTML,但这些都没有区别。 I ave even tried using one equal sign instead of two; 我甚至试过用一个等号而不是两个; however, that would make all criteria true, regardless if it were true or not. 然而,这将使所有标准成立,无论它是否真实。

Thanks 谢谢

DFM DFM

if(document.getElementById("myID").innerHTML == "1")

Would evaluate to true if the element in question (eg a div) had the following markup. 如果有问题的元素(例如div)具有以下标记,则评估为true。

<div>1</div>

You could also use jQuery 你也可以使用jQuery

if($(element).text() == "1")

You have onCLick instead of onclick (all lowercase) in your second example, which is why it is not working. 在第二个示例中,您有onCLick而不是onclick (全部小写) ,这就是它无法正常工作的原因。 You should use innerHTML to get the text inside the <p> element. 您应该使用innerHTML来获取<p>元素中的文本。

Note however, that were you to have other elements inside of an element that you wanted to get the text for, using innerHTML will return all of those elements and their respective content too. 但是请注意,如果要在要获取文本的元素中包含其他元素,则使用innerHTML将返回所有这些元素及其各自的内容。

Working Demo 工作演示

This should do what you want. 这应该做你想要的。

<html>

  <input id="test" type="text" value="hello">

  <script>
    if (document.getElementById('test').value == "hello") alert("hello");
  </script>

</html>

It's been a couple years since I've been deep in JavaScript development, but as I recall (a bit rusty), instead of innerHTML, it's better to use stuff like childNodes[0].nodeValue or firstChild.nodeValue for 自从我开始深入JavaScript开发已有几年了,但是我记得(有点生疏),而不是innerHTML,最好使用像childNodes [0] .nodeValue或firstChild.nodeValue这样的东西。

tags like that. 这样的标签。

Refer to the following link... http://www.howtocreate.co.uk/tutorials/javascript/dombasics 请参阅以下链接... http://www.howtocreate.co.uk/tutorials/javascript/dombasics

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

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