Disable Hyerperlink in a Div using Jquery
I have div with Textbox, label and hyperlink it it. I want to disable /enable the elements in it. I am using this code.
$('#Content_2 :input').attr('disabled', true);
Content_2 - is a div
But this leaves the hyerperlink and label enabled. How c开发者_Python百科an modify this ?
:input
is only for input type (<input>
, <textarea>
, <select>
and <button>
) elements, for the others you'll need to add to the selector:
$('#Content_2').find(':input, label').attr('disabled', true);
You can also do one selector, I just find this a bit cleaner to look at, the single selector would be:
$('#Content_2 :input, #Content_2 label').attr('disabled', true);
Base on comments: Now for the anchors, that one's a little different, you're better off binding the events unobtrusively, for example:
$('#Content_2').delegate('img.toggle:not(.disabled)', 'click', function(e) {
window.scroll(150,650);
tooglecargoLoading();
});
Then that link can just be the <img>
itself, like this:
<img class="toggle" src="images/accept_but1.jpg" width="119" height="20" border="0" />
To enable/disable it, just add/remove the disabled
class, like this:
$("#Content_2 img.toggle").addClass("disabled");
//to re-enable:
$("#Content_2 img.toggle").removeClass("disabled");
By doing this, the link will/won't match the selector when the click
event bubbles...meaning it'll be effectively disabled. There are also other ways to do this, like putting the
$('#Content_2 :input').attr('disabled', true);
only disables all <input /> element inside #Content_2 not <a>
try
$('#Content_2 :input,#Content_2 a,#Content_2 span').attr('disabled', 'disabled');
to enable them use:
$('#Content_2 :input,#Content_2 a,#Content_2 span').removeAttr('disabled');
精彩评论