javascript JQuery单击跨度文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5766438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:16:38  来源:igfitidea点击:

JQuery click text in span?

javascriptjquerycsshtml

提问by Eric

I'm trying to use JQuery to have a method fire when the following code gets clicked:

我正在尝试使用 JQuery 在单击以下代码时触发方法:

<div class="content_sub_list">
    <img src="/imgs/close-icon.jpg" class="exit_sub_list"/>
    hello
</div>

It is not doing anything and my JQuery code looks like this:

它没有做任何事情,我的 JQuery 代码如下所示:

    $(document).ready(
function() 
{ 
    $('#my_accnt').click(
    function() 
    {
      $('.content_sub_list').css("display", "block");
      $('.content_sub_list').css("margin-left", "4px");
      $('.content_sub_list').html('<div class="sub_list_header">My A<img src="/imgs/close-icon.jpg" class="exit_sub_list"/></div><BR />text');
    });
    $('#my_queue').click(
    function() 
    {
      $('.content_sub_list').css("display", "block");
      $('.content_sub_list').css("margin-left", "165px");
      $('.content_sub_list').html('<div class="sub_list_header">My Q<img src="/imgs/close-icon.jpg" class="exit_sub_list"/></div><BR />text');
    });
    $('.exit_sub_list').click(
    function() 
    {
        alert("hi");
      $('.content_sub_list').css("display", "none");
    });
});

My header looks like this:

我的标题看起来像这样:

    <script src="/js/jquery.js" type="text/javascript"></script>
    <script src="/js/mContentBar.js" type="text/javascript"></script>

Other functions work in it except this one? for some reason. So what am I doing wrong, or how i can get this to work? Thanks

除了这个功能还有其他功能吗?由于某些原因。那么我做错了什么,或者我怎样才能让它发挥作用?谢谢

回答by Hussein

This is working for me. Make sure you have jQuery loaded properly. You can either place jquery.js after your html or wrap your jquery code with a document.ready function.

这对我有用。确保您已正确加载 jQuery。您可以将 jquery.js 放在 html 之后,也可以使用 document.ready 函数包装 jquery 代码。

like

喜欢

(function() {
    $('.exit_sub_list').click(

    function() {
        alert("hi");
    });
});

Check working example at http://jsfiddle.net/p6Q2d/

http://jsfiddle.net/p6Q2d/检查工作示例