jquery学习第四番 常问的问题

本文介绍了jQuery中各种选择器的使用方法,包括ID选择器、类选择器和标签选择器等,并详细解释了如何利用这些选择器进行元素的操作,如获取值、设置值、检查元素状态等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,如何使用选择器

 

 $('#myDivId')            id选择器
 $('.myCssClass')        class选择器
$('div')                        标签选择器
var myDivElement = $('#myDivId');       //一个jquery的object
 var myValue = $('#myDivId').val();    // get the value of a form input
 
 $('#myDivId').val("hello world");     // set the value of a form input

How do I test whether an element has a particular class?

 第一种方式
$("div").click(function(){
   if ( $(this).hasClass("protected") )
     $(this)
       .animate({ left: -10 })
       .animate({ left: 10 })
       .animate({ left: -10 })
       .animate({ left: 10 })
       .animate({ left: 0 });
 });
第二种方式
 if ( $('#myDiv').is('.pretty.awesome') )
   $('#myDiv').show();
测试一个元素是否被hidden,使用 :hidden 当然还有 :visible
 if ( $('#myDiv').is(':hidden') )
   $('#myDiv').show();

How do I test whether an element exists

 if ( $('#myDiv').length )
   $('#myDiv').show();
当然你也可以直接 $('#myDiv').show();而不会有任何问题。

How do I determine the state of a toggled element?

 var isVisible = $('#myDiv').is(':visible');
 var isHidden = $('#myDiv').is(':hidden');
$('#myDiv:visible').animate({left: '+=200px'}, 'slow');

How do I select an element by an ID that has characters used in CSS notation?

 // Does not work
 $("#some:id")
 
 // Works!
 $("#some//:id")
 // Does not work
 $("#some.id")
 
 // Works!
 $("#some//.id")

The following function takes care of escaping these characters and places a "#" at the beginning of the ID string:

 function jq(myid) { 
   return '#' + myid.replace(/(:|/.)/g,'//$1');
 }


The function can be used like so:

 $( jq('some.id') )

How do I disable/enable a form element?

 // Disable #x
 $('#x').attr('disabled', true);
 // Enable #x
 $('#x').attr('disabled', false);

Add or remove the 'disabled' attribute:

 // Disable #x
 $("#x").attr('disabled', 'disabled');
 // Enable #x
 $("#x").removeAttr('disabled');

How do I get the text value of a selected option?

<select id="myselect">
   <option value="1">Mr</option>
   <option value="2">Mrs</option>
   <option value="3">Ms</option>
   <option value="4">Dr</option>
   <option value="5">Prof</option>
 </select>
 <input type="button" value="Get Value" onclick="alert($('#myselect').val())"/>
 <input type="button" value="Get Text Value" onclick="alert($('#myselect option:selected').text())"/>

 

How do I replace text from the 3rd element of a list of 10 items?

 

Either the :eq() selector or the .eq() method will allow you to select the proper item. However, to replace the text, you must get the value before you set it:

  // This doesn't work; text() returns a string, not the jQuery object
  $(this).find('li a').eq(2).text().replace('foo','bar');

  // This works
  var $thirdLink = $(this).find('li a').eq(2);
  var linkText = $thirdLink.text().replace('foo','bar');
  $thirdLink.text(linkText);

The first example just discards the modified text. The second example saves the modified text and then replaces the old text with the new modified text. Remember, .text() gets.text("foo")sets.


效果

 // Instead of this:
 $("span").show("slow");
 
 // do this:
 $("span").fadeIn("slow");      淡入
fadeOut('slow');                      淡出

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值