中获取 'text'-6ren"> 中获取 'text'-我无法让它在 IE 中运行: HTML 代码如下所示: Red: id 在本例中为:document.getElementById('red_input'); 我只想返回“红色”一词。它在 FF 中-6ren">
gpt4 book ai didi

javascript - 如何从

Text<input type="...

中获取 'text'

转载 作者:行者123 更新时间:2023-12-02 20:43:59 24 4
gpt4 key购买 nike

我无法让它在 IE 中运行:

HTML 代码如下所示: <p class="red">Red: <input type="text" id="red_input" value="0"></p>

id 在本例中为:document.getElementById('red_input');

我只想返回“红色”一词。它在 FF 中运行良好,但在 IE 中则不然。

function error(id){

var prnt = id.parentNode,
parentColor = prnt.childNodes[0].textContent,
trimmed = parentColor.replace(/\s+/g,'').replace((/\:$/),"").toLowerCase();

return trimmed
}

在 FF 中它返回字符串“red”在 IE 中,它返回“未定义为 null 或不是对象”。我认为“textContent”在 IE 中似乎不起作用。有人有什么建议吗?

非常感谢任何帮助!

最佳答案

在 IE 中,您应该得到 innerText属性:

function error(id){
var prnt = id.parentNode.childNodes[0],
parentColor = prnt.textContent || prnt.innerText,
trimmed = parentColor.replace(/\s+/g,'').replace((/\:$/),"").toLowerCase();

return trimmed;
}

请注意,我使用了逻辑或运算符 (||),因此如果 textContent 未定义(或为假),它将查找 innerText

关于javascript - 如何从 <p>Text&lt;input type="... </p> 中获取 'text',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1775749/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com