hello5world
2016-04-08 10:30:40
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>無(wú)標(biāo)題文檔</title><script src="jquery.js"></script><script> function check(){ var $mobile = $("#shouji"); if($mobile.attr("value") == ""){ var oht = "<font>內(nèi)容為空</font>"; $mobile.append(oht); } return true; }</script></head><body> 手機(jī)號(hào)碼驗(yàn)證:<input id="shouji" type="text" value="" onblur="return check()"/><br/></body></html>
3 回答

ruibin
TA貢獻(xiàn)358條經(jīng)驗(yàn) 獲得超213個(gè)贊
此處存在的唯一問(wèn)題是input為單標(biāo)簽。而jquery中append函數(shù)的本質(zhì)是設(shè)置innerHTML,既然是但標(biāo)簽,那么就沒(méi)有innerHTML所以這里append無(wú)效。改進(jìn)方法是:將$mobile.append(oht);換成$mobile.attr('value', oht)。不過(guò)你這個(gè)font寫在這里沒(méi)有意義,建議刪除。

走向流沙
TA貢獻(xiàn)45條經(jīng)驗(yàn) 獲得超23個(gè)贊
append是追加內(nèi)容到元素節(jié)點(diǎn)之間(最后面),你的這個(gè)value是他的屬性,不是元素節(jié)點(diǎn)。
你寫的這語(yǔ)句$mobile.append(oht);改成$mobile.attr("value",oht);
添加回答
舉報(bào)
0/150
提交
取消