本文介绍了javascript函数在IE 7中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人都可以告诉我如何在IE 7中运行此脚本?当我运行它时,没有任何反应。
Can anyone please tell me how do I make this script run in IE 7? When I run this , nothing happens.
<html> <head> <script language="JavaScript"> function moveSelectedOption() { // Fetch references to the <select> elements. var origin = document.getElementById('origin_select'); var target = document.getElementById('target_select'); // Fetch the selected option and clone it. var option = origin.options[origin.selectedIndex]; var copy = option.cloneNode(true); // Add the clone to the target element. target.add(copy, null); } </script> </head> <body> <select id="origin_select" multiple> <option value="A">A</option> <option value="B">B</option> <option value="C">C</option> </select> <select id="target_select" multiple> <option value="C1">C1</option> </select> <button onclick="moveSelectedOption()">Add</button> <!-- <input type="button" onClick="moveSelectedOption()" value="AddMeToo" /> this does not work either--> </body> </html>推荐答案
尝试
var origin = document.getElementById('origin_select'); var target = document.getElementById('target_select'); // Fetch the selected option and clone it. var option = origin.options[origin.selectedIndex]; target.options[target.options.length] = new Option(option.text, option.value);如果你想从原因选择元素中删除该选项,那么你可以使用这个
If you want to remove the option from the origin select element then you can use this
origin.remove(option);没有移动的演示
Demo without move
移动演示
Demo with move
编辑
Edit
此行导致错误。
target.add(copy, null);
add()不适用于标准秒在资源管理器中的参数,即使值为null,所以为了兼容,可以尝试双参数版本,并且在失败时,使用单参数版本。 / p>
add() does not work with the standard second argument in Explorer, even with value null, so in order to be compatible one may try the two-argument version and upon failure, use the single argument version.
参见 select.add
See select.add
更多推荐
javascript函数在IE 7中不起作用
发布评论