0

我创建了这个小书签来突出显示页面上的用户名和密码框。我可以更改框的颜色,但如果我尝试更改框中的文本,它就不起作用:

<a href="javascript:void(var boxes= $(':text, :password');var selectionBox = $(':password');selectionBox.val('password');for(var i = 0; i < boxes.length;i++){if(boxes[i] == selectionBox[0]){boxes.eq(i-1).val('login');}})">Password box highlighter</a>

我试过了.text = ''.value = ''.val('')

谢谢。

4

1 回答 1

2

Wrap the code to execute into a function-call:

<a href="javascript:void(function(){var boxes= $(':text, :password');var selectionBox = $(':password');selectionBox.val('password');for(var i = 0; i < boxes.length;i++){if(boxes[i] == selectionBox[0]){boxes.eq(i-1).val('login');}}}())">Password box highlighter</a>

Explanation: void expects an expression(only 1 expression).

An expression is any valid unit of code that resolves to a value

Your code already breaks with the first var-keyword, because declaring a variable is not an expression.

When you wrap the code into a function-call, the expression is the function-call itself, no matter which code will be executed inside the function.

于 2013-06-24T08:00:17.307 回答