Javascript 的“this”问题关键词

发布于 2024-11-19 23:34:19 字数 871 浏览 2 评论 0原文

我有以下 HTML 标记:

<a class="link1" href="javascript:load(0,1);">Click here</a><span class="loader"></span>

加载函数定义如下:

function load(flag,id){

    /*Load Forum*/
    if(flag==0){
        $(this).next("span.loader").html("<img src='./images/loader.gif'/>");
    console.log("Here");
    //....Some code follows

所以我想要的是预加载器 gif 应在单击链接时出现在链接旁边。 “console.log”正在将“此处”打印到控制台,但图像没有出现。该图像位于正确的地址。我认为我使用“this”关键字的方式有问题。任何帮助将不胜感激。谢谢。 :)

更新:正如一些人指出的那样,我通过将 this 作为参数传递来传递对该元素的引用。现在这是我的代码,但它仍然无法工作:

<a class="link1" href="javascript:load(0,1,this);">Click here</a><span class="loader"></span>


function load(flag,id,ref){
/*Load Forum*/
if(flag==0){
    if(ref){ $(ref).next("span.loader").html("123");console.log("Here"); }

I have the following HTML markup :

<a class="link1" href="javascript:load(0,1);">Click here</a><span class="loader"></span>

The load function is defined as follows :

function load(flag,id){

    /*Load Forum*/
    if(flag==0){
        $(this).next("span.loader").html("<img src='./images/loader.gif'/>");
    console.log("Here");
    //....Some code follows

So what I want is the preloader gif should appear beside the link when it is clicked. The 'console.log' is printing 'Here' to the console but the image is not appearing. The image is located at the correct address. I think there is problem in the way I am using the 'this' keyword. Any help would be appreciated. Thanks. :)

Updated: As it was pointed out by some, I passed reference to the element by passing this as an argument. Here is my code now but it does not work still :

<a class="link1" href="javascript:load(0,1,this);">Click here</a><span class="loader"></span>


function load(flag,id,ref){
/*Load Forum*/
if(flag==0){
    if(ref){ $(ref).next("span.loader").html("123");console.log("Here"); }

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

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

发布评论

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

评论(7

雅心素梦 2024-11-26 23:34:19

如果您使用 jQuery 绑定事件处理程序而不是使用“href”(无论如何这都是一个非常坏的习惯),那么您的 this 值将是正确的。

$(function() {
  $('a.link1').click(function(e) {
    e.preventDefault();
    if ($(this).data('flag') === '0') {
      $(this).next('span.loader').html('<img/>', { src: './images/loader.gif' });
      // ...
    }
  });
});

这也意味着标记将会改变:

<a class='link1' href='#' data-flag='0'>Click here</a>

通过 jQuery 绑定处理程序意味着库将确保 this 引用正在为其调用处理程序的元素;即 标记。按照您的方式(使用“href”伪 URL),情况并非如此。

If you bind your event handler with jQuery instead of using the "href" (which is a really bad habit anyway), then your this value would be correct.

$(function() {
  $('a.link1').click(function(e) {
    e.preventDefault();
    if ($(this).data('flag') === '0') {
      $(this).next('span.loader').html('<img/>', { src: './images/loader.gif' });
      // ...
    }
  });
});

That also implies that the markup would change:

<a class='link1' href='#' data-flag='0'>Click here</a>

Binding the handler through jQuery means that the library will make sure that this refers to the element for which the handler is being invoked; that is, the <a> tag. The way you have it (with the "href" pseudo-URL), that would not be the case.

优雅的叶子 2024-11-26 23:34:19

你的问题的答案是使用 javascript:load.call(this, 0, 1) 但 Pointy 是对的。

The answer to your question is to use javascript:load.call(this, 0, 1) but Pointy is right.

花之痕靓丽 2024-11-26 23:34:19

上下文是 window 因为您使用 javascript: 伪协议引用它。我会不引人注目地附上它。

<a id="link" href="#">jkfds</a>
<script>
   (function(){
        var l = document.getElementById('link');
        l.onclick = function() {
           load.call( this, 0, 1 );
           return false; // if needed
        };
        function load(one,two){ alert(one); alert(two); alert(this.nodeName); }
   })();
</script>

上下文默认为锚点,因为我们不在全局上下文中执行它,因为匿名函数附加到 DOMElement (锚点节点)并且上下文通过 .call 传递。

如果您需要 DOM 元素本身的参数,只需使用 data- 属性并使用 .getAttribute 引用它们(假设它在 IE6 中一致工作)

The context is window because you're referencing it with the javascript: pseudo protocol. I would unobtrusively attach it.

<a id="link" href="#">jkfds</a>
<script>
   (function(){
        var l = document.getElementById('link');
        l.onclick = function() {
           load.call( this, 0, 1 );
           return false; // if needed
        };
        function load(one,two){ alert(one); alert(two); alert(this.nodeName); }
   })();
</script>

The context defaults to the anchor since we're not executing it in global context, since the anonymous function is attached to the DOMElement ( anchor node ) and the context is passed via .call.

If you need the parameters on the DOM element itself, just use data- attributes and reference them with .getAttribute ( assuming it works consistently in IE6 )

简美 2024-11-26 23:34:19

尝试:

<a class="link1" href="#" flag="0">Click here</a><span class="loader"></span>

$("a.link1").click(function() {
    if($(this).attr("flag") ==0){
        $(this).next("span.loader").html("<img src='./images/loader.gif'/>");
        console.log("Here");
    }
})

Try:

<a class="link1" href="#" flag="0">Click here</a><span class="loader"></span>

and

$("a.link1").click(function() {
    if($(this).attr("flag") ==0){
        $(this).next("span.loader").html("<img src='./images/loader.gif'/>");
        console.log("Here");
    }
})
初见你 2024-11-26 23:34:19

未经测试的

<a class="link1">Click here</a><span class="loader"></span>

    $('.link1').click(function() {
        load(flag, id);
    }

    function load(flag,id){

        /*Load Forum*/
        if(flag==0){
            $(this).next("span.loader").html("<img src='./images/loader.gif'/>");
        console.log("Here");
        //....Some code follows
    }

untested

<a class="link1">Click here</a><span class="loader"></span>

    $('.link1').click(function() {
        load(flag, id);
    }

    function load(flag,id){

        /*Load Forum*/
        if(flag==0){
            $(this).next("span.loader").html("<img src='./images/loader.gif'/>");
        console.log("Here");
        //....Some code follows
    }
星星的軌跡 2024-11-26 23:34:19

Pointy 是对的,有更好的方法可以做到这一点,但如果您无法重构,您可以将 this 作为 javascript 调用的另一个参数传递...

Pointy is right that there are better ways to do this, but if you can't refactor, you can pass this as another parameter of your javascript call...

眼趣 2024-11-26 23:34:19

这是一个工作示例,假设您在多个位置使用 load(flag, id) 函数:http://jsfiddle.net/jfriend00/GRcXS/。主要问题是“this”的值是浏览器窗口,而不是使用 javascript: 分配事件处理程序时的元素。既然你有 jQuery,最好使用 jQuery 来连接点击事件,然后它会适当地设置 this 点。

HTML:

<a class="link1" data-flag="0" href="#">Click here</a>
<span class="loader"></span>

JavaScript:

function load(flag,id){
    /*Load Forum*/
    if(flag == 0) {
        $(this).next("span.loader").html("<img src='http://photos.smugmug.com/photos/344291068_HdnTo-Ti.jpg'/>");
        console.log("Here");
        //....Some code follows
    }
    return(false);
}

$(".link1").click(function() {
    return(load.call(this, $(this).data("flag"), 1));
});

Here's a working example that assumes you were using the load(flag, id) function multiple places: http://jsfiddle.net/jfriend00/GRcXS/. The main issue was that the value of "this" is the browser window, not the element when you assign the event handler using javascript:. Since you have jQuery, it's much better to use jQuery to hook up the click event and it will then set the this point appropriately.

HTML:

<a class="link1" data-flag="0" href="#">Click here</a>
<span class="loader"></span>

Javascript:

function load(flag,id){
    /*Load Forum*/
    if(flag == 0) {
        $(this).next("span.loader").html("<img src='http://photos.smugmug.com/photos/344291068_HdnTo-Ti.jpg'/>");
        console.log("Here");
        //....Some code follows
    }
    return(false);
}

$(".link1").click(function() {
    return(load.call(this, $(this).data("flag"), 1));
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文