每个JavaScript和DOS填充select元素时IE的问题 [英] Problem with IE when filling a select element per JavaScript andDOM

查看:58
本文介绍了每个JavaScript和DOS填充select元素时IE的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


在我的html表单中,我显示了一个select元素,如果单击此元素,我会填写

每个JavaScript / DOM

with option-elements。我使用以下代码:


<!DOCTYPE HTML PUBLIC" - // W3C // DTD HTML 4.01 Transitional // EN"

" http://www.w3.org/TR/html4/loose.dtd">

< html>

< head>

< title>< / title>

< script language =" JavaScript" type =" text / javascript">

函数onWordlistSelect()

{

var selectElement = document.getElementById(9);

if(selectElement.getAttribute(" populated")==" 0")

{

for(var i = 0; i < 5; i ++)

{

var optionElement = document.createElement(" option");

var wordlistEntryName =" MyOption_" ; + i;

var textNode = document.createTextNode(wordlistEntryName);

optionElement.appendChild(textNode);

selectElement.appendChild(optionElement) ;

}

selectElement.setAttribute(" populated"," 1");

}

}

< / script>

< / head>

< body>

< select风格= QUOT;宽度:150像素"大小= QUOT 1 QUOT;填充= QUOT; 0" onClick =" onWordlistSelect()"

id =" 9">

< option>< / option>

< / select>

< / body>

< / html>


这适用于Mozilla:

当我点击它打开的下拉列表时,我可以选择其中一个添加的选项。

但是我有以下行为:

当我点击下拉列表时,它会立即打开并关闭,所以我不能

选择一个条目。

我必须再次点击查看我的选项我添加并选择其中一个

他们。


有没有人知道如何解决这个问题?

Thanx提前!


奥利弗

Hello,

In my html form I show a select-element and if this element is clicked I fill it
per JavaScript/DOM
with option-elements. I use the following code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<script language="JavaScript" type="text/javascript">
function onWordlistSelect()
{
var selectElement = document.getElementById(9);
if (selectElement.getAttribute("populated") == "0")
{
for( var i = 0; i < 5; i++)
{
var optionElement = document.createElement( "option" );
var wordlistEntryName = "MyOption_" + i;
var textNode = document.createTextNode(wordlistEntryName);
optionElement.appendChild(textNode);
selectElement.appendChild(optionElement);
}
selectElement.setAttribute("populated", "1");
}
}
</script>
</head>
<body>
<select style="width:150px" size="1" populated="0" onClick="onWordlistSelect()"
id="9">
<option></option>
</select>
</body>
</html>

This works fine with Mozilla:
when I click on the dropdown it is opened and I can choose one of the added options.
BUT with IE I have the following behaviour:
when I click on the dropdown it is opened and closed immediately so I cannot
choose an entry.
I have to make a second click to see the options I''ve added and to select one of
them.

Does anyone have an idea how I could solve this problem?
Thanx in advance!

Oliver

推荐答案

Oliver Hauger写道:
Oliver Hauger wrote:
你好,

在我的html表单中,我显示了一个select元素,如果单击此元素,我将根据JavaScript / DOM填充它

元素。我使用以下代码:

<!DOCTYPE HTML PUBLIC" - // W3C // DTD HTML 4.01 Transitional // EN"
" http://www.w3。 org / TR / html4 / loose.dtd">
< html>
< head>
< title>< / title>
< script language = " JavaScript的" type =" text / javascript">
函数onWordlistSelect()
{var selectElement = document.getElementById(9);


根据HTML规范,ID属性可以包含数字,但是它不能以数字开头(尽管我认为大多数浏览器都会容忍)它)。


if(selectElement.getAttribute(" populated")==" 0")
{
for(var i = 0; i < 5; i ++)
{var optionElement = document.createElement(" option");
var wordlistEntryName =" MyOption_" + i;
var textNode = document.createTextNode(wordlistEntryName);
optionElement.appendChild(textNode);


代替使用:


var txt = wordlistEntryName;

var optionElement = new Option(txt);


新选项的语法是:


新选项([text [,value [,defaultSelected [,selected]]]])


其中defaultSelected&选中的是布尔值(真/假)。

selectElement.appendChild(optionElement);
}
selectElement.setAttribute(" populated"," 1");


选择元素没有''填充''属性。我不确定这个

做了什么有用的事情,结果可能不一致或不可靠。

[...]

但是与IE我有以下行为:
当我点击下拉列表时,它立即打开和关闭,所以我
无法选择一个条目。
我必须再次点击查看选项我已添加并选择其中一个。

有谁知道如何解决这个问题?
Hello,

In my html form I show a select-element and if this element is clicked I
fill it per JavaScript/DOM
with option-elements. I use the following code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<script language="JavaScript" type="text/javascript">
function onWordlistSelect()
{
var selectElement = document.getElementById(9);
According to the HTML spec, an ID attribute can contain numbers but it
can''t start with a number (though I think most browsers will tolerate it).

if (selectElement.getAttribute("populated") == "0")
{
for( var i = 0; i < 5; i++)
{
var optionElement = document.createElement( "option" );
var wordlistEntryName = "MyOption_" + i;
var textNode = document.createTextNode(wordlistEntryName);
optionElement.appendChild(textNode);
use instead:

var txt = wordlistEntryName;
var optionElement = new Option(txt);

Syntax of new Option is:

new Option ([ text [, value [, defaultSelected[, selected ]]]])

where defaultSelected & selected are booleans (true/false).
selectElement.appendChild(optionElement);
}
selectElement.setAttribute("populated", "1");
select elements do not have a ''populated'' attribute. I''m not sure this
does anything useful, the results are likely not consistent or reliable.
[...]
BUT with IE I have the following behaviour:
when I click on the dropdown it is opened and closed immediately so I
cannot choose an entry.
I have to make a second click to see the options I''ve added and to
select one of them.

Does anyone have an idea how I could solve this problem?




使用 - 新选项 - 如上所述。


-

Rob



Use -- new Option -- as above.

--
Rob


>
< select style =" width:150px"大小= QUOT 1 QUOT;填充= QUOT; 0"的onClick = QUOT; onWordlistSelect()" id =" 9">
<select style="width:150px" size="1" populated="0" onClick="onWordlistSelect()" id="9">




代替使用:


< select style =" width:150px"大小= QUOT 1 QUOT; populated =" 0"

onmouseover =" onWordlistSelect()" id =" 9">



use instead:

<select style="width:150px" size="1" populated="0"
onmouseover="onWordlistSelect()" id="9">


你好RobG,


thanx你的回复!

我现在已按照您的建议更改了我的代码:


<!DOCTYPE HTML PUBLIC" - // W3C // DTD HTML 4.01 Transitional // EN"

" http://www.w3.org/TR/html4/loose.dtd">

< html>

< head>

< title>< / title>

< script language =" JavaScript" type =" text / javascript">

function onWordlistSelect()

{

var selectElement = document.getElementById(" sel_9" );
for(var i = 0; i< 5; i ++)

{

var wordlistEntryName =" MyOption_" + i;

var optionElement = new Option(wordlistEntryName);

selectElement.appendChild(optionElement);

}

}

< / script>

< / head>

< body>

< ; select style =" width:150px"大小= QUOT 1 QUOT;的onClick = QUOT; onWordlistSelect()" id =" sel_9">

< option>< / option>

< / select>

< / body> ;

< / html>


但是我和以前的IE有同样的问题和使用

新选项时(txt)

创建选项元素选项'的文字没有显示。

我也删除了填充的属性我已经改变了id属性为

请确保这不会导致错误!


更多想法如何解决问题!?

Oliver

RobG写道:
Hello RobG,

thanx for your reply!
I''ve now changed my code as you''ve suggested:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<script language="JavaScript" type="text/javascript">
function onWordlistSelect()
{
var selectElement = document.getElementById("sel_9");
for( var i = 0; i < 5; i++)
{
var wordlistEntryName = "MyOption_" + i;
var optionElement = new Option(wordlistEntryName);
selectElement.appendChild(optionElement);
}
}
</script>
</head>
<body>
<select style="width:150px" size="1" onClick="onWordlistSelect()" id="sel_9">
<option></option>
</select>
</body>
</html>

BUT I have the same problem with the IE as before AND when using
new option(txt)
to create the option element the option''s text is not shown.
I''ve also removed the populated attribute and I''ve changed the id attribute to
be sure that this does not cause the error!

Any more ideas how to solve the problem!?

Oliver
RobG wrote:
Oliver Hauger写道:
Oliver Hauger wrote:
你好,
在我的html表单中,我显示了一个select元素,如果点击了这个元素,我会用JavaScript / DOM
填充选项元素。我使用以下代码:

<!DOCTYPE HTML PUBLIC" - // W3C // DTD HTML 4.01 Transitional // EN"
" http://www.w3。 org / TR / html4 / loose.dtd">
< html>
< head>
< title>< / title>
< script language = " JavaScript的" type =" text / javascript">
函数onWordlistSelect()
{var selectElement = document.getElementById(9);
Hello,

In my html form I show a select-element and if this element is clicked
I fill it per JavaScript/DOM
with option-elements. I use the following code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<script language="JavaScript" type="text/javascript">
function onWordlistSelect()
{
var selectElement = document.getElementById(9);



依据对于HTML规范,ID属性可以包含数字,但它不能以数字开头(虽然我认为大多数浏览器都会容忍它)。


According to the HTML spec, an ID attribute can contain numbers but it
can''t start with a number (though I think most browsers will tolerate it).

if(selectElement.getAttribute(" populated")==" 0")
{
for(var i = 0; i< 5; i ++)
{
var optionElement = document.createElement(" option");
var wordlistEntryName =" MyOption_" + i;
var textNode =
document.createTextNode(wordlistEntryName);
optionElement.appendChild(textNode);
if (selectElement.getAttribute("populated") == "0")
{
for( var i = 0; i < 5; i++)
{
var optionElement = document.createElement( "option" );
var wordlistEntryName = "MyOption_" + i;
var textNode =
document.createTextNode(wordlistEntryName);
optionElement.appendChild(textNode);



代替使用:

var txt = wordlistEntryName;
var optionElement = new Option(txt);

新选项的语法是:

新选项([text [,value [,defaultSelected [,selected]]]])

其中defaultSelected&选择的是布尔值(真/假)。


use instead:

var txt = wordlistEntryName;
var optionElement = new Option(txt);

Syntax of new Option is:

new Option ([ text [, value [, defaultSelected[, selected ]]]])

where defaultSelected & selected are booleans (true/false).

selectElement.appendChild(optionElement);
}
selectElement.setAttribute(" populated"," 1" );
selectElement.appendChild(optionElement);
}
selectElement.setAttribute("populated", "1");



选择元素没有''填充''属性。我不确定这是否有用,结果可能不一致或不可靠。

[...]


select elements do not have a ''populated'' attribute. I''m not sure this
does anything useful, the results are likely not consistent or reliable.
[...]

但是与IE我有以下行为:
当我点击下拉列表时,它立即打开和关闭,所以我
无法选择一个条目。
我必须再次点击查看选项我已添加并选择其中一个。

有谁知道如何解决这个问题?
BUT with IE I have the following behaviour:
when I click on the dropdown it is opened and closed immediately so I
cannot choose an entry.
I have to make a second click to see the options I''ve added and to
select one of them.

Does anyone have an idea how I could solve this problem?



使用 - - 新选项 - 如上所述。


Use -- new Option -- as above.



这篇关于每个JavaScript和DOS填充select元素时IE的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆