---
title: "jquery 编程注意问题"
author: "烈火"
date: "2015-04-09T14:55:44Z"
tags: []
categories: ["Web前端"]
slug: "jquery"
draft: false
lastmod: "2015-04-09T15:10:07Z"
id: 2200576
url: "https://burningout.iteye.com/blog/2200576"
---

###### 1.判断我的选择是否包含元素？

// 不要这样做！

if ( $( "div.foo" ) ) {

}

// 请使用length属性

if ( $( "div.foo" ).length ) {

}

###### 2.需要重复调用同一个选择时，请缓存你的选择：

var divs = $( "div" );

###### 3.获取dom对象

$( "#foo" )\[ 0 ];

###### 4.与select相关

$( "#myselect" ).val();

$( "#myselect option:selected" ).text();

###### 5.与checkbox/radio相关

$( "#x" ).prop( "checked", true );

$( "#x" ).prop( "checked", false );

###### 6.Disabled

$( "#x" ).prop( "disabled", true );

$( "#x" ).prop( "disabled", false );