gpt4 book ai didi

javascript - 0.style 为 null 或不是对象

转载 作者:行者123 更新时间:2023-12-02 18:29:53 25 4
gpt4 key购买 nike

我通过以下方式在java脚本中编写了代码。

function changeRoomType()
{

var rtype = document.getElementById('typeofroom').value;
var ps = document.getElementsByName('radios');
if(rtype == 'sb' || rtype == 'db')
{
ps[0].style.display = 'block';
ps[1].style.display = 'block';
}
else
{
ps[0].style.display = 'none';
ps[1].style.display = 'none';
}
}

我的html代码是

<p style="position: absolute; float: left; bottom: 138px; width: 50px; left: 359px;display:none;"
name='radios'>
<label for="class">Superior</label>
<input type="radio" name="radio" id="class" value="s" tabindex="1" style="float:left; width:25px;background-color:transparent !important;"/>
</p>
<p style="position: absolute; float: left; bottom: 138px; width: 50px; left: 287px;display:none;"
name='radios'>
<label for="class2">Premier</label>
<input type="radio" name="radio" id="class2" value="p" tabindex="1" style="float:left; width:25px;background-color:transparent !important;"/>
</p>

当在写入 onchange 事件的选择框中选择值时,将调用上述函数。通过在选择框中选择房间类型,会显示两个单选按钮。这在 google chrome 和 mozilla 中运行良好。但在 Internet Explorer8 中我收到错误
0.style 为 null 或不是对象。并且不显示单选按钮。

提前致谢!

最佳答案

发布了相关问题和解决方案 here 。请引用MSDN documentation on getElementsByTagName 。 MSDN 上的此文档讨论了 getElementsByName 的行为受 **expando ** 属性的影响。

通过控制 expando 属性,可以定义 IE 如何处理不属于 HTML 元素标准属性集的属性。请参阅 Expando on MSDN

Documentation for the p tag表明 name 属性不是该标签标准属性的一部分;因此它是一个扩展属性。因此,虽然 Mozilla 和 Chrome 对这种扩展的属性和工作感到满意;在您在 Javascript 中设置 document.expando=true 之前,IE 不会识别此属性。请注意,这需要通过检查浏览器名称来有条件地完成。

作为事后的想法 - 为什么不使用 jQuery 来执行此类 DOM 操作?您将不再为编写跨浏览器兼容的 JavaScript 代码而烦恼,并且可以专注于您的网站功能。

关于javascript - 0.style 为 null 或不是对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17907589/

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