Run jQuery function onclick

前端 未结 4 523
梦如初夏
梦如初夏 2021-02-08 02:28

so i implemented a bit of jQuery that basically toggles content via a slider that was activated by an tag. now thinking about it id rather have the DIV th

相关标签:
4条回答
  • 2021-02-08 02:32

    Using obtrusive JavaScript (i.e. inline code) as in your example, you can attach the click event handler to the div element with the onclick attribute like so:

     <div id="some-id" class="some-class" onclick="slideonlyone('sms_box');">
         ...
     </div>
    

    However, the best practice is unobtrusive JavaScript which you can easily achieve by using jQuery's on() method or its shorthand click(). For example:

     $(document).ready( function() {
         $('.some-class').on('click', slideonlyone('sms_box'));
         // OR //
         $('.some-class').click(slideonlyone('sms_box'));
     });
    

    Inside your handler function (e.g. slideonlyone() in this case) you can reference the element that triggered the event (e.g. the div in this case) with the $(this) object. For example, if you need its ID, you can access it with $(this).attr('id').


    EDIT

    After reading your comment to @fmsf below, I see you also need to dynamically reference the target element to be toggled. As @fmsf suggests, you can add this information to the div with a data-attribute like so:

    <div id="some-id" class="some-class" data-target="sms_box">
        ...
    </div>
    

    To access the element's data-attribute you can use the attr() method as in @fmsf's example, but the best practice is to use jQuery's data() method like so:

     function slideonlyone() {
         var trigger_id = $(this).attr('id'); // This would be 'some-id' in our example
         var target_id  = $(this).data('target'); // This would be 'sms_box'
         ...
     }
    

    Note how data-target is accessed with data('target'), without the data- prefix. Using data-attributes you can attach all sorts of information to an element and jQuery would automatically add them to the element's data object.

    0 讨论(0)
  • 2021-02-08 02:32

    Why do you need to attach it to the HTML? Just bind the function with hover

    $("div.system_box").hover(function(){ mousin }, 
                              function() { mouseout });
    

    If you do insist to have JS references inside the html, which is usualy a bad idea you can use:

    onmouseover="yourJavaScriptCode()"
    

    after topic edit:

    <div class="system_box" data-target="sms_box">
    

    ...

    $("div.system_box").click(function(){ slideonlyone($(this).attr("data-target")); });
    
    0 讨论(0)
  • 2021-02-08 02:33

    You can bind the mouseenter and mouseleave events and jQuery will emulate those where they are not native.

    $("div.system_box").on('mouseenter', function(){
        //enter
    })
    .on('mouseleave', function(){
        //leave
    });
    

    fiddle

    note: do not use hover as that is deprecated

    0 讨论(0)
  • 2021-02-08 02:49

    There's several things you can improve upon here. To start, there's no reason to use an <a> (anchor) tag since you don't have a link.

    Every element can be bound to click and hover events... divs, spans, labels, inputs, etc.

    I can't really identify what it is you're trying to do, though. You're mixing the goal with your own implementation and, from what I've seen so far, you're not really sure how to do it. Could you better illustrate what it is you're trying to accomplish?

    == EDIT ==

    The requirements are still very vague. I've implemented a very quick version of what I'm imagining you're saying ... or something close that illustrates how you might be able to do it. Left me know if I'm on the right track.

    http://jsfiddle.net/THEtheChad/j9Ump/

    0 讨论(0)
提交回复
热议问题