0

In my website I have buttons that are loaded via AJAX. How can I ensure that my jQuery hide() operation is applied to all buttons that are inserted into the DOM in future? As far as I know on() works only when you register events like click for example.

<button class="myButtton">Button</button>

JavaScript:

$(".myButton").hide();
4

3 回答 3

1
.myButton {
  display: none;
}

While adding the content apply myButton class to the element

于 2013-05-17T12:56:55.923 回答
0

you can use the jquery function .on() This will apply the behavior also to element that are added to the dom later.

$(document).on('click','.elementClass' , function(e){
     // do something
});
于 2013-05-17T08:52:42.667 回答
0

It's easier to just use css to have a hidden button upon being inserted. jQuery.hide() is basically in css display: none. http://api.jquery.com/hide/.

.myButton {
  display: none;
}

Then when you are ready to show it just use $(".myButton").show(); aka display: block.

于 2013-05-17T12:53:58.947 回答