开发者

Remove html element which not contains specific class

开发者 https://www.devze.com 2023-04-04 13:54 出处:网络
I am trying to remove li\'s with not active state, but cant make it work: var html = $(\"#myjqgriddiv\").remove(\"ul li:not(.ui-state-active)\").html();

I am trying to remove li's with not active state, but cant make it work:

var html = $("#myjqgriddiv").remove("ul li:not(.ui-state-active)").html();

I have html and i want to remove li which not contains class .ui-state-active

After code above the li with .ui-state-active still there.

Need help.

UPDATE:

My intention to that html i am going to print using window.open("","Print")/document.write

@tvanfosson answer is working but there is one more little thing, the single li which stayed after first modification i want to edit as well i want to remove attribute "href" from its a c开发者_StackOverflow中文版hild

var html = $('#myjqgriddiv') // select div
                .find('ul li:not(.ui-state-active)') // select elements to remove from div
                .remove()  // remove the matched elements
                .end() // revert back to the originally selected elements
                .html(); // and get the HTML


html = $(html).find("ul li.ui-state-active a)").removeAttr("href").html(); // not working?!


The argument to remove acts as a filter, not a selector. Since you've only selected the one element with id mygqgriddiv, the filter removes nothing. Try.

 var html = $('#myjqgriddiv') // select div
                .find('ul li:not(.ui-state-active)') // select elements to remove from div
                .remove()  // remove the matched elements
                .end() // revert back to the originally selected elements
                .find("ul li.ui-state-active a)") // now get the active ones
                .removeAttr("href") // remove the href attribute
                .end() // back to the original selection
                .html(); // and get the HTML


jQuery's remove will only remove elements from the selected list. Put the two selectors into the $ function and remove the result.

var html = $("#myjqgriddiv ul li:not(.ui-state-active)").remove().html();
0

精彩评论

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

关注公众号