使用 JavaScript 和 XPath 从 DOM 中选择元素

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

Selecting element from DOM with JavaScript and XPath

javascriptdomxpath

提问by Eric Herlitz

I'm trying to figure out how to select the textarea in the code below using xpath and JavaScript (which is the only option here).

我试图弄清楚如何使用 xpath 和 JavaScript(这是这里唯一的选项)在下面的代码中选择 textarea。

<body>
    <div id="calculator">
        <div id="calculatorController">
            <form action="#" method="get" onsubmit="return false">
                <p>
                    <textarea disabled="disabled"></textarea>
                </p>
            </form>
        ...

I'm trying to do something like this

我正在尝试做这样的事情

var element = document.evaluate( '//body/form/p/textarea' ,document, null, XPathResult.ANY_TYPE, null );
// and write back
element.value = "Hello textarea";

But it fails

但它失败了

Anyone keen to help?

有人热心帮忙吗?

Thanks

谢谢

Update below this

下面更新

============================================================

================================================== ==========

The entire block of code looks like this. Don't forget the window.onload=function()

整个代码块看起来像这样。不要忘记window.onload=function()

<html>
<head>
  <script type='text/javascript'> 
  //<![CDATA[ 
  window.onload=function(){
  var element = document.evaluate( '//body//form/p/textarea' ,document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue;

        if (element != null) {
            element.value = 'Hello textarea';
        }

  }
  //]]> 
  </script> 
</head>
<body>
    <div id="calculator">
        <div id="calculatorController">
            <form action="#" method="get" onsubmit="return false">
                <p>
                    <textarea disabled="disabled"></textarea>
                </p>
            </form>
        </div>
    </div>
</body>
</html>

回答by Martin Honnen

The evaluate method does not return a DOM node as you seem to expect. You would need

评估方法不会像您预期的那样返回 DOM 节点。你需要

var element = document.evaluate( '//body//form/p/textarea' ,document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue;
if (element != null) {
  element.value = '...';
}

回答by Yann Milin

@Mark Robinson comment is right, your Xpath expression is wrong, you could use one of those :

@Mark Robinson 评论是对的,您的 Xpath 表达式是错误的,您可以使用其中之一:

//body/div/div/form/p/textarea (Mark's example)
//body//form/p/textarea (any form in body)

Plus, the evaluate function will return a XPathResultobject, not the textarea, so you can't do directly element.value

另外,评估函数将返回一个XPathResult对象,而不是 textarea,所以你不能直接做 element.value

Here is your example fixed:

这是您修复的示例:

<body>
    <div id="calculator">
        <div id="calculatorController">
            <form action="#" method="get" onsubmit="return false">
                <p>
                    <textarea disabled="disabled"></textarea>
                </p>
            </form>
        </div>
    </div>
</body>

--

——

var element = document.evaluate( '//body/div/div/form/p/textarea' ,document, null, XPathResult.ANY_TYPE, null );

var textarea = element.iterateNext ();
textarea.value = "Hello textarea";