javascript – 将“document.getElementById”转换为jQuery

前端之家收集整理的这篇文章主要介绍了javascript – 将“document.getElementById”转换为jQuery前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在思考/寻找下面我遇到的问题,而且找不到任何东西.
有没有办法可以重写这个以与jQuery一起使用?

代码的前半部分.

  1. <a href="link.PHP?test=true" onclick="request(this)" target="_blank">
  2.  
  3. <a id="step2" href="javascript:alert('NOT FINISHED!');">

下半部分代码.

  1. <script language="javascript">
  2. var requests = 16;
  3.  
  4. function request(self)
  5. {if(self.href != "#")
  6. requests -= 1;
  7.  
  8. self.childNodes[0].src = "images/clear.gif";
  9.  
  10. if(requests === 0)
  11. document.getElementById("step2").href = "next.PHP";}
  12. </script>

而我想做一个jQuery var请求类型的东西.我想onclick =“请求(这)与我的jQuery一起工作.

解决方法

您的问题(标题)的答案是替换
  1. document.getElementById('about').href = '#';

  1. $('#about').attr('href','#');

我不知道这是否真的会帮助你解决问题,因为我真的不知道你想要做什么.根据您的评论代码示例,您似乎正在尝试执行某种向导,但我不知道在您发布的内容后它将如何实际工作.

更新:

也许是这样的:

  1. <a href="link.PHP?test=true" onclick="request(this)" target="_blank" class='request'></a>
  2.  
  3. <a id="step2" href="next.PHP">Step 2</a>
  4.  
  5. <script language="javascript">
  6. $(function() {
  7. var requests = 16;
  8. $('#step2').click( function() {
  9. alert('Not finished');
  10. return false;
  11. });
  12. $('.request').click( function() {
  13. var $this = $(this); // save jQuery reference to element clicked
  14.  
  15. // swap indicator image source
  16. $this.find('img:first').attr('src','images/clear.gif');
  17.  
  18. // if the number of flipped indicators equals the number of requests
  19. // remove the click handler from the step 2 link
  20. // this removes the alert and allows the user to proceed
  21. if ($('.request img[src="images/clear.gif"]').length == requests) {
  22. $('#step2').unbind('click');
  23. }
  24.  
  25. ...do something with the href for this request via ajax???
  26.  
  27. // replace this handler with one that simply returns false
  28. // and remove the href since we're done with this request
  29. $(this).unbind('click').attr('href','#').click( function() { return false; } );
  30.  
  31. // stop the default action for the request
  32. return false;
  33. });
  34. </script>

猜你在找的jQuery相关文章