标签或标签中网页上的任何文字吗?
tag or tag be selected programmatically?
can any text on a webpage in a
可以在网页上的任何文字
tag or tag be selected programmatically? the input element and textarea element can do that easily, but how about text in other cases?
标记或标记是否以编程方式选择? input元素和textarea元素可以很容易地做到这一点,但在其他情况下文本怎么样?
You can find an example of getting inner text from a <p>
tag here. Same thing with a <span>
.
您可以在此处找到从
标记获取内部文本的示例。 也是如此。
To set it you just need to assign the InnerText property.
要设置它,您只需要分配InnerText属性。
If your javascript snippet is out of scope (in a function etc) use the GetElementById method of the document global object to retrieve your <p>
or <span>
or whatever - in fact you can do this with any element provided you assign an id to the element.
如果您的javascript片段超出范围(在函数等中),请使用文档全局对象的GetElementById方法来检索
或或其他 - 事实上,如果您指定了id,则可以使用任何元素执行此操作对元素。
JohnIdol's answer will work in all modern browsers except Firefox. See here for a browser compatibility chart on innerText
. As the link also shows, you can use textContent
to get the same thing in FF.
JohnIdol的答案适用于除Firefox以外的所有现代浏览器。有关innerText上的浏览器兼容性图表,请参见此处。如链接所示,您可以使用textContent在FF中获取相同的内容。
The suggestion to use getElementById
is an excellent one, but it's good to note that you can still access the text of an element if it doesn't have an id attached to it, which is frequently the case with <p>
and <span>
tags. For example if you know the 4th <p>
tag has the text you want you can do the following:
使用getElementById的建议是一个很好的建议,但是请注意,如果元素的文本没有附加id,你仍然可以访问它的文本,这通常是
和的情况。标签。例如,如果您知道第4个
标记包含您想要的文本,则可以执行以下操作:
var p = document.getElementsByTagName('p')[3];
var text = p.innerText || p.textContent;
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2009/05/18/228256ea5a63d2a84ac94e3983a246f6.html。