简体   繁体   English

Bookmarklet没有找到输入框

[英]Bookmarklet not finding input box

I have made a bookmarklet that should highlight the password and username box on a webpage. 我制作了一个书签,应该突出显示网页上的密码和用户名框。 It finds the password box, and seems to be locating the text box that is before it, but then it stops working. 它找到了密码框,似乎找到了它之前的文本框,但随后它停止工作。 Here is my code: 这是我的代码:

<html>
<head>
</head>
<body>
<a href="javascript:var boxes= $(':text, :password');var selectionBox = $(':password'); selectionBox.css('background','red');for(var i = 0; i < boxes.length;i++){alert('loop');if(boxes[i] == selectionBox[0]){alert('Username box found');boxes[i-1].css('background','blue');alert('Success!');}}">Password box highlighter</a>
</body>

I get an alert box saying 'loop' a couple of times, then 'Username box found', but then it stops working on the next line, and no 'Success!' 我得到一个警告框,说'循环'几次,然后'找到用户名框',但是它停止在下一行工作,没有'成功!' alert box. 警报框。 Does anybody know what I am doing wrong? 有谁知道我做错了什么? Thanks. 谢谢。

EDIT: Here is the code spread out: 编辑:这是扩展的代码:

var boxes= $(':text, :password');
var selectionBox = $(':password');
selectionBox.css('background','red');
for(var i = 0; i < boxes.length;i++){
 alert('loop');
 if(boxes[i] == selectionBox[0]){
  alert('Username box found');
  boxes[i-1].css('background','blue');
  alert('Success!');
 }
}

You're using boxes[i-1] (which is the same as using .get(i-1) ) to get a DOM element from the jQuery collection. 您正在使用boxes[i-1] (与使用.get(i-1) )从jQuery集合中获取DOM元素。 Try using .eq() : 尝试使用.eq()

boxes.eq(i-1).css('background','blue');

DEMO: http://jsfiddle.net/HsDCs/3/ 演示: http //jsfiddle.net/HsDCs/3/

If you checked your browser's console, you would've seen that it was complaining that css isn't a property/method for that element. 如果你检查了浏览器的控制台,你会发现它抱怨css不是该元素的属性/方法。

References: 参考文献:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM