将“document.getElementById”转换为进入 jQuery

发布于 2024-08-04 17:42:02 字数 673 浏览 3 评论 0原文

我一直在思考/寻找与下面遇到的问题相同的问题,但什么也没找到。 有没有办法可以重写它以使用 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 一起使用。

I've been thinking/searching for an equivalent of the problem that I am having below, and nothing could be found.
Is there a way I can rewrite this to work with jQuery as an alternative?

First half of the code.

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

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

Second half of the code.

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

Whereas I want to do a jQuery var request type thing. I want onclick="request(this) to work with my jQuery.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凉栀 2024-08-11 17:42:03

像这样?我想我错过了/不明白一些事情......

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

Like this? I think I'm missing/not understanding something though...

$('#about').click( function(evt) {
   request(this);
 });
五里雾 2024-08-11 17:42:03

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

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

然后只需将所有调用从 更改为

document.getElementById

$.getElementById

这有很多注意事项,但根据您的情况,它可能很有用。

For convenience you can define an additional method on the $ object

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

you can then just change all of your calls from

document.getElementById

to

$.getElementById

There are tons of caveats with this but it can be useful depending on your situation.

陌伤浅笑 2024-08-11 17:42:03

这?不确定我完全明白你想要的

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

调用方式:

onclicked="return request(this);"

This? Not sure I fully get what you want

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

invoked by:

onclicked="return request(this);"
当梦初醒 2024-08-11 17:42:02

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

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>

The answer to your question (title) is to replace

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

with

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

I have no idea if this will actually help you solve your problem, though, since I really don't know what you are trying to do. Based on your comments and code sample, it appears that you are trying to do some sort of wizard, but I have no idea how it would actually work given what you've posted.

Update:

Maybe something like:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文