6

我一直在思考/寻找与我在下面遇到的问题相同的问题,但找不到任何东西。有没有办法可以重写它以使用 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="request(this)使用我的 jQuery。

4

4 回答 4

10

您的问题(标题)的答案是替换

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>
于 2009-09-12T00:09:54.450 回答
2

像这样?我想我错过/不理解一些东西......

$('#about').click( function(evt) {
   request(this);
 });
于 2009-09-11T23:53:30.413 回答
2

为方便起见,您可以在 $ 对象上定义一个附加方法

$.getElementById = function(id){
    return $('#'+id).get(0);
};

然后你可以改变你所有的电话

document.getElementById

$.getElementById

对此有很多警告,但根据您的情况,它可能很有用。

于 2012-01-25T18:19:54.697 回答
0

这?不确定我是否完全得到你想要的

function request(element)
{
    $(element).doSomething();
    return false;
}

调用者:

onclicked="return request(this);"
于 2009-09-11T23:56:18.693 回答