JavaScript和JQuery获取DIV的值
时间:2015-03-13 00:29:29
收藏:0
阅读:11011
1、设计源码
<!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=utf-8" /> <title>JavaScript和JQuery获取DIV的值</title> <script type="text/javascript" src="jquery-1.11.2.js"></script> <script type="text/javascript"> $(function(){ $("#jqueryInput").click(function(){ var divValue = $("#div_val").text(); var divVal = $("#div_val").html(); alert("JQuery获取DIV的值:"+divValue+"\n"+divVal); }); }); function javaScriptDiv() { var divVal = document.getElementById("div_val").innerHTML; alert("JavaScript获取DIV的值:"+divVal); } </script> </head> <body> <div id="div_val">youhaidong</div> <input type="button" value="JavaScript获取DIV的值" onclick="javaScriptDiv()"/> <input type="button" value="JQuery获取DIV的值" id="jqueryInput"/> </body> </html>
2、运行结果
(1)初始化
(2)点击”JavaScript获取DIV的值“按钮
(3)点击”JQuery获取DIV的值“按钮
3、分析说明
(1)JavaScript获取DIV的值
document.getElementById("div_val").innerHTML;
(2)JQuery获取DIV的值
a $("#div_val").text();
b $("#div_val").html();
评论(0)