Javascript 浏览器独立性问题代码适用于IE,但不适用于Firefox和Chrome
如果我为IE运行相同的代码,那么一切都很好,我得到的是selectedId的值,而对于firefox和chrome,它给出的值是未定义的Javascript 浏览器独立性问题代码适用于IE,但不适用于Firefox和Chrome,javascript,html,internet-explorer,firefox,google-chrome,Javascript,Html,Internet Explorer,Firefox,Google Chrome,如果我为IE运行相同的代码,那么一切都很好,我得到的是selectedId的值,而对于firefox和chrome,它给出的值是未定义的 ----------------- code ------------------------------- <html> <head> <script type="text/javascript"> function createSelected() { var value; var theForm = documen
----------------- code -------------------------------
<html>
<head>
<script type="text/javascript">
function createSelected()
{
var value;
var theForm = document.hell;
for (var i = 0; i < theForm.length; i++)
{
var e = theForm.elements[i];
if ((e.type == "hidden") && (e.value == "false"))
{
console.log("the value of selected IDS="+e.selectedId);
if (e.selectedId!= undefined )
{
Value = ", "+e.selectedId;
}
}
}
console.log( Value);
}
</script>
</head>
<body>
<form name="hell">
<h1>This working only with IE not with FireFox and Crome </h1>
<br/>
<br/>
<input type="hidden" selectedId="heyya1" name="item1" value="false">h1</input>
<input type="hidden" selectedId="heyya2" name="item2" value="false">h2</input>
<input type="hidden" selectedId="heyya3" name="item3" value="false">h3</input>
<input type="hidden" selectedId="heyya4" name="item4" value="false">h4</input>
<input type="hidden" selectedId="heyya5" name="item5" value="false">h5</input>
<input type="hidden" selectedId="heyya6" name="item6" value="false">h6</input>
<input type="button" onclick=createSelected() value="find the values"></input>
</form>
</body>
---------------------------code end-----------------------------------------
--------------代码-------------------------------
函数createSelected()
{
var值;
var theForm=document.hell;
对于(var i=0;i
h1
氢
h3
h4
h5
h6
---------------------------代码端-----------------------------------------
请在这方面提供帮助,为什么我们不能在FireFox中使用其他参数(如HTML标记中的Selectedid),就像在IE中一样
提前谢谢 尝试读取所选的ID,如下所示。使用getAttribute方法 警报(“所选ID的值=“+e.getAttribute('selectedId')) 在Chrome中(对不起,我使用的是一台没有FF的计算机,我不会仅仅为了这个问题而安装它),如果你使用它,它就可以工作。因此: 或者,在您的功能中:
var id = e.getAttribute("selectedId");
console.log("the value of selected IDS="+id );
if (id != null)
{
value += ", "+id;
}
另外,请注意JavaScript是区分大小写的,因此您的变量
value
与value
不一样,您所说的value=
可能指的是value+=
。您可能希望在元素属性周围使用引号:onclick=“createSelected()”
它真的有效吗???我想知道。为什么不只是一个简单的id=heyya…
而不是这个自定义属性呢?它适用于IE,但不适用于fire fox。
var id = e.getAttribute("selectedId");
console.log("the value of selected IDS="+id );
if (id != null)
{
value += ", "+id;
}