JavaScript 递归无法正常工作
谁能说为什么以下递归函数对我不起作用? 它应该递归地收集给定元素中的所有单选按钮。 但是,由于某种原因它没有找到任何!?
谢谢 !!
<?xml version="1.0" encoding="Windows-1255"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
function AllInputs(radioElement) {
this.radioInputs = ((arguments.length == 1) ? [radioElement] : []);
}
AllInputs.prototype.toString = function() {
return "[object AllInputs: radioInputs: " + this.radioInputs.length + "]";
}
AllInputs.prototype.add = function(otherAllInputs) {
this.radioInputs = this.radioInputs.concat(otherAllInputs.radioInputs);
}
function getAllInputsOfElement(element) {
if (element.tagName.toLowerCase() == "input") {
if (element.getAttribute("type").toLowerCase() == "radio") {
return new AllInputs(element);
} else {
return new AllInputs();
}
} else {
var result = new AllInputs();
for (i = 0; i < element.children.length; i++) {
result.add(getAllInputsOfElement(element.children[i]));
}
return result;
}
}
function main() {
alert(getAllInputsOfElement(document.getElementById("MyTable")));
}
</script>
</head>
<body onload="main()">
<table id="MyTable">
<tr><td>Day</td></tr>
<tr><td>
<input type="radio" name="DayOfTheWeek" value="1" /><label>Monday</label>
<input type="radio" name="DayOfTheWeek" value="2" /><label>Tuesday</label>
<input type="radio" name="DayOfTheWeek" value="3" /><label>Wednesday</label>
</td></tr>
</table>
</body>
</html>
Could anyone say why the following recursive function does not work for me ?
It should collect recursively all radio buttons in a given element.
But, it does not found any for some reason !?
Thanks !!
<?xml version="1.0" encoding="Windows-1255"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
function AllInputs(radioElement) {
this.radioInputs = ((arguments.length == 1) ? [radioElement] : []);
}
AllInputs.prototype.toString = function() {
return "[object AllInputs: radioInputs: " + this.radioInputs.length + "]";
}
AllInputs.prototype.add = function(otherAllInputs) {
this.radioInputs = this.radioInputs.concat(otherAllInputs.radioInputs);
}
function getAllInputsOfElement(element) {
if (element.tagName.toLowerCase() == "input") {
if (element.getAttribute("type").toLowerCase() == "radio") {
return new AllInputs(element);
} else {
return new AllInputs();
}
} else {
var result = new AllInputs();
for (i = 0; i < element.children.length; i++) {
result.add(getAllInputsOfElement(element.children[i]));
}
return result;
}
}
function main() {
alert(getAllInputsOfElement(document.getElementById("MyTable")));
}
</script>
</head>
<body onload="main()">
<table id="MyTable">
<tr><td>Day</td></tr>
<tr><td>
<input type="radio" name="DayOfTheWeek" value="1" /><label>Monday</label>
<input type="radio" name="DayOfTheWeek" value="2" /><label>Tuesday</label>
<input type="radio" name="DayOfTheWeek" value="3" /><label>Wednesday</label>
</td></tr>
</table>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个,
我不会检查你想要解决的问题。我刚刚检查了您遇到的问题。
这是因为 getAllInputsOfElement() 中迭代变量 i 的范围。
变量 i 未声明为方法的本地变量,它在全局范围内可用。因此,只需使用 var 关键字将变量声明为本地变量即可。
尝试使用 firefug 等任何其他调试工具来检查 JavaScript 执行情况以修复此类问题。
尝试放置一些日志消息以找出实际的执行路径并对其进行分析以查找代码执行的问题
希望这可以解决您的问题
Try this
I'm not checking what you are trying to solve. I just checked the issue you are having.
It is because of the scope of the iterating variable i in getAllInputsOfElement().
The variable i not declared as local to the method, it is available in the global scope. So just declare the variable as local using var keyword.
Try to use firefug for any other debugging tools to check the javascript execution to fix this kind of issues.
Try to put some logging messages to find out actual execution path and analyse it to find the issues with the code execution
Hope this solves your problem
对于一个可以使用 DOM 方法轻松处理的问题,您已经有了一个非常复杂的解决方案。试试这个:
使用 jQuery 可以进一步简化。
You've got a very complicated solution to a problem that is easily handled with DOM methods. Try this instead:
It could be simplified even more with jQuery.