简体   繁体   English

onclick HTML和jQuery不起作用

[英]onclick html and jquery doesn't work

I have a problem with my jQuery and html code, when I click in the <a> element that contains the onclick event nothing happens. 我的jQuery和html代码有问题,当我单击包含onclick事件的<a>元素时,什么也没有发生。

This is the html code 这是HTML代码

<a href='http://gomusic.designerbh.com/profile/bar_53d6af107ee03/'>Cabana</a>
<a href='javascript:void(0)' onclick='actus("actuevent_53d6e48c65b68")' style='float:right;width:24px;height:12px;background:url(http://gomusic.designerbh.com/img/arrow.png) no-repeat bottom center'></a>
<span style='float:right;color:#ccc;margin:0px 10px 0px 0px'>49 Minutes ago</span>
<div class='actusevent_53d6e48c65b68' style='z-index:1;position:absolute;width:200px;padding:10px;background:#fff;margin:10px 0px 0px 260px;border:1px solid #ccc;border-radius:5px'>content here</div>

This is the JS code, when I use this code nothing happens 这是JS代码,当我使用此代码时,没有任何反应

function actus(b){
    $(".actus"+b).hide();
}

But when I do this the code alerts the value 122 and works fine 但是,当我这样做时,代码将警告值122并正常工作

function actus(b){
    alert('122');
}

I already debugged it. 我已经调试了。 I'm using the latest version of Chrome and jquery-1.9.1.min.js 我正在使用最新版本的Chrome和jquery-1.9.1.min.js

you have wrong select javascript, try 您选择的JavaScript错误,请尝试

function actus(b){
            $(".actus"+b).hide();
        }

  onclick='actus("event_53d6e48c65b68")'

Your parameter value is different from the name of your class. 您的参数值不同于类的名称。

Note onclick='actus("actuevent_53d6e48c65b68")' and class='actusevent_53d6e48c65b68' . 注意onclick='actus("actuevent_53d6e48c65b68")'class='actusevent_53d6e48c65b68'

Another thing: the element you're trying to get in actus() would certainly not match your parameter value. 另一件事:您尝试在actus()获取的元素肯定与您的参数值不匹配。 i suggest you change your parameter value to start with "event_" instead of "actuevent_", or you can change the prefix of the element in actus() to be: $(".actusevent_") . 我建议您将参数值更改为以“ event_”而不是“ actuevent_”开头,或者可以将actus actus()元素的前缀更改为: $(".actusevent_")

EDIT To Barmars comment below - you could simply split it: 编辑下面的Barmars评论-您可以将其拆分:

function actus(b){
    var i = b.split('_');
    $(".actusevent_"+b[1]).hide();
}

Look at your class name, it includes an underscore - _ . 看看你的类名,它包含下划线- _ What you want is this: 您想要的是:

function actus(b){
    $(".actusevent_"+b).hide();
}

Errors: 错误:

  • You're trying to call $('.actus' + b).hide() when your class is actually called actusevent . 当您的类实际上称为actusevent时,您正在尝试调用$('.actus' + b).hide()
  • You're also missing the _ in your jQuery function. 您还缺少jQuery函数中的_ So it won't ever work. 所以它永远都行不通。

There are more efficient ways of doing this. 有更有效的方法可以做到这一点。 If you have control over how you create your DOM elements, you could create something like this: 如果您可以控制创建DOM元素的方式,则可以创建以下内容:

<a href='javascript:void(0)' id="my-link" data-actus-event='53d6e48c65b68' style='float:right;width:24px;height:12px;background:url(http://gomusic.designerbh.com/img/arrow.png) no-repeat bottom center'></a>

Now create your bind: 现在创建绑定:

$(document).on('click', 'a#my-link', function(e){
    e.preventDefault();
    var actusevent = $(this).data('actus-event');

    $('.actusevent_' + actusevent).hide();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM