开发者

Apply Css for elements even after postback-jquery

I have many hrefs(with d开发者_开发知识库ynamic Ids) in my asp.net app that have the same CssClass=MyClass.

I want these button to be hidden with a condition.

I used the .ready

$(document).ready(function() {
  if(condition)
    $('.MyClass').css("display","none");
});

the problem is docuement.ready doesn't execut when there is a poctback.

Postback==>Button visible.normal as i've put the code in .ready.

Is there a way to persist the code:$('.MyClass').css("display","none");

I tried to apply .live() on button load,but it doesn't work.

Any ideas?

Thanks in advance.


You can take a different approach, define the style in CSS, like this:

body.conditionClass .MyClass { display: none; }

Then apply that class to <body> on document.ready, like this:

$(function() {
  if(condition)
    $('body').addClass('conditionClass');
});

Now new elements with .MyClass, anywhere in the <body> will get the display: none styling.


Use the jQuery livequery plugin: http://brandonaaron.net/code/livequery/docs

live() only binds events. When you have installed the plugin, use:

$('.MyClass') 
.livequery(function() { 
    $(this).css("display","none");
});

This will hide items of class MyClass whenever they are found, even if they are created from a Ajax response. You can even use this code in place of the ready function you are currently using.

In this case, Nick Craver's solution is better, but only if you have to evaluate condition just on page load.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜