微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我一直在思考/寻找下面我遇到的问题,而且找不到任何东西.
有没有办法可以重写这个以与jQuery一起使用?

代码的前半部分.

<a href="link.PHP?test=true" onclick="request(this)" target="_blank">

<a id="step2" href="javascript:alert('NOT FINISHED!');">

下半部分代码.

<script language="javascript">
var requests = 16;

function request(self)
{if(self.href != "#")
requests -= 1;

self.childNodes[0].src = "images/clear.gif";

if(requests === 0)
document.getElementById("step2").href = "next.PHP";}
</script>

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

解决方法

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

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

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

更新:

也许是这样的:

<a href="link.PHP?test=true" onclick="request(this)" target="_blank" class='request'></a>

<a id="step2" href="next.PHP">Step 2</a>

<script language="javascript">
    $(function() {
       var requests = 16;
       $('#step2').click( function() {
           alert('Not finished');
           return false;
       });
       $('.request').click( function() {
           var $this = $(this); // save jQuery reference to element clicked

           // swap indicator image source
           $this.find('img:first').attr('src','images/clear.gif');

           // if the number of flipped indicators equals the number of requests
           // remove the click handler from the step 2 link
           // this removes the alert and allows the user to proceed
           if ($('.request img[src="images/clear.gif"]').length == requests) {
              $('#step2').unbind('click');
           }

           ...do something with the href for this request via ajax???

           // replace this handler with one that simply returns false
           // and remove the href since we're done with this request
           $(this).unbind('click').attr('href','#').click( function() { return false; } );

            // stop the default action for the request
           return false;
    });
</script>

原文地址:https://www.jb51.cc/jquery/156026.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐