为什么此脚本中的警报不起作用?

时间:2014-04-04 17:05:14

标签: javascript html alert var

我希望将文档中给出的文本的一些信息转换为变量,并将它们插入警报进行验证,但警报不起作用。为什么会这样?

以下是代码的一部分:

<head>
<script type="text/javascript">
window.onload = function() {

var enlaces = document.getElementsByTagName("a");
var numero = enlaces.length();
var dir = enlaces[numero-2].href;
var contador = 0;

for (var i = 0; i <= enlaces.length-1; i++) {
    if(enlaces[i].href = "http://prueba" || enlaces[i].href = "http://prueba/")
        contador++;
};

var parrafos = document.getElementsByTagName("p");
var enlacesTercerParrafo = parrafos[2].getElementsByTagName("a").length;

alert("El numero de enlaces es:");

}
</script>
</head>

2 个答案:

答案 0 :(得分:5)

.length之后不应该有();它应该是这样的:

var numero = enlaces.length;

答案 1 :(得分:3)

尝试==运算符进行比较

if(enlaces[i].href == "http://prueba" || enlaces[i].href == "http://prueba/")