2009-09-11 62 views
6

我一直在思考/搜索下面存在的问题的等价物,并且找不到任何东西。 有没有一种方法可以重写这个以使用jQuery作为替代方法?将“document.getElementById”转换为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一起工作。

+1

更改href很容易,但我不知道你真的想做什么。也许你想要完成什么的解释会有所帮助。仅仅改变href就像一个动作一样毫无意义。 – tvanfosson 2009-09-12 00:06:02

+0

我有5个按钮,并且我希望它每次按下按钮时都会请求它,以便在满足请求后,它将更改“id(about)”的href。目前href =“#”允许他们不继续,这么说呢? – Homework 2009-09-12 00:08:21

回答

10

的回答你的问题(标题)是替代

document.getElementById('about').href = '#'; 

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

我不知道这是否实际上h尽管如此,我仍然不知道你在做什么。根据你的评论和代码示例,看起来你正在尝试做某种向导,但是我不知道如何根据你发布的内容实际工作。

更新:

也许是这样的:

<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> 
2

是否这样?我认为我失踪/不理解某事虽然...

$('#about').click(function(evt) { 
    request(this); 
}); 
0

This?不知道我完全得到你想要的

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

援引什么:

onclicked="return request(this);" 
+0

要求?它是什么?链接页面? – tvanfosson 2009-09-12 00:03:58

+0

为什么不只是完全删除锚标签,所以他们甚至不能点击它? – tvanfosson 2009-09-12 00:11:01

2

为了方便,你可以在$对象上定义一个额外的方法

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

然后你可以只改变所有从您的通话

document.getElementById 

$.getElementById 

有吨这一警告的,但它可以根据您的情况是有用的。

+0

你能否提供一些关于警告的提示 – nus 2012-08-03 23:21:06