开发者

Why my Jquery function not working?

开发者 https://www.devze.com 2023-02-20 02:30 出处:网络
I have this jquery function --- function开发者_如何转开发 domail_slide_settings(obj) { $(\"div#\" + obj).toggle();

I have this jquery function ---

function开发者_如何转开发 domail_slide_settings(obj)
{
  $("div#" + obj).toggle();
}

and when I execute it like this ---

<a href="#" onclick="domail_slide_settings('toggle')" id="toggle" >Toggle</a>

It doesn't toggle! Does anyone have any ideas about this. Please let me know, it would be a great help.

Thanks in advance!


try with

  $("#" + obj).toggle();

and point it to a < div > element, instead of an anchor


ID attributes must be unique in a HTML document, otherwise you will run into unwanted side-effects. From the looks of it, your <a> element with the click handler has the same id attribute as the div#toggle element you're toggling. This would almost certainly be a problem in some browsers.

Try changing the ID of your <a> element to be unique:

<a href="#" onclick="domail_slide_settings('toggle')" id="toggle_div" >Toggle</a>


Try this instead:

<a href="#" id="toggle">Toggle</a>
<div id="div_toggle"></div>

and this:

$(function() {
    $("#toggle").click(function(e){
        e.preventDefault();
        $("#div_" + this.id).toggle();
        // or $(this).next().toggle(); depending on your markup
    });
});

I bet you're using the same id for your <a> tag and your <div> tag which should not be done.


or you can try

$('#' + obj).parent().toggle();

^^


the result of your string concatenation - "div#toggle" - is selecting a div with id toggle, instead of your anchor.

I think you meant to select it as:

$("#toggle").parent()

or, if you want really what the anchor itself, use this:

$("#toggle")

In any case, since your function is an onclick handler (directly through the browser though, not using jQuery event api), you might as well do this:

function domail_slide_settings() {
    $(this).parent().toggle()
    // or this.toggle();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消