jQuery 怎么获取获取form div下 div的innerHTML?

他的最新文章
他的热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)javascript获取div的内容 精华篇
转载 & & 作者:
用js获取div的内容的方法,其实原理就是利用innerText或innerHTML
原理:采用innerText 或者 innerHTML
代码如下:&script language=”javascript”& var stock_code = stockcode.innerT var stock_code = stockcode.innerHTML; &/script& &div id="stockcode" style="display:none"& test &/div& innerText 跟 innerHTML是两个非DOM标准的方法其区别如图所示:(图中应该为innerText)在IE中 innerText 跟 inner HTML 两个方法都能正常运行但是FF里面的innerText不可用,但是有一个替代方法: textContentIE: oDiv.innerText = aS oDiv.innerHTML = aSFF: oDiv.textContent = aS oDiv.innerHTML = aSAjax in action 的作者之一Eric 用正则表达式 实现了 一个兼容方法,比较有趣Hope this helpsA little smirk One day a secretary is leaving on her lunch break, and she notices her boss standing in front of a shredder with a clueless look on his face. The secretary walks up to him and asks if he needs help. "Yes!" he says looking and sounding relieved, "This is very important." Glad to help, she turns the shredder on and inserts the paper. Then her boss says, "Thanks, I only need one copy." Create function like innerText As you may have figured out innerText is IE only. That means that browsers like Mozilla, Firefox, and Netscape will return undefined. If you do not know what innerText does, it strips out all of the tags so you only see the text. For example, if a div contains the HTML &span id='span1'&Eric&/span&, innerHTML would return &span id='span1'&Eric&/span& while innerText will return Eric. Now to make innerHTML act the same we need to use some regular expressions with the strings replace() method. Now the basic pattern we need to match is or or or Now the regular expression we need to use is /&\/?[^&]+&/gi If you do not know regular expressions here is a quick explanation: / - Starts the regular expression & - Match the less than sign \/ - Escape the character / so it can be matched (Without the \ you would be saying it is the end of the reg exp.) ? - Match the / character 0 or 1 times [^&] - Match any character but greater than sign + - Match [^&] one or more times & - Match greater than sign / - End the regular expression gi - Tells regular expression to match global and ignore the case So now the function to replace the text would look like:
代码如下:&script type="text/javascript"& var regExp = /&\/?[^&]+&/ function ReplaceTags(xStr){ xStr = xStr.replace(regExp,""); return xS } &/script& All you need to do is pass it a string and it returns the string stripped of the tags. An example is shown below to grab the text from a div without the tags.
Test Test Test
[Ctrl+A 全选 注:]
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具*jQuery中设置或者获取所选内容的值:text();设置或者获取所选元素的文本内容;
html();设置或者获取所选元素的内容(包括html标记);
val();设置或者获取表单字段的值(前提是表单设置了value属性);
(text()和html()的区别是:前者是处理的文本内容,只能写文本如果写了上面的标记则会以文本形式输出;后者可以解析文本中的html标记,就是你可以添加像&a&&/a&、&p&&/p&等标记,最后会解析为其效果。)
*JavaScript中设置或者获取所选内容的值
同理innerText、innerHTML和value,
innerText和innerHTML都是将字符串放入hmtl标签中的一个函数但是innerHTMl他可以解析hmtl标记例如 你放入一个&a&&/a&& 如果在DIV中它里面就会出现一个带下划线的a元素;但是innerText只支持普通字符串;
*具体代码如下:jQuery中:
&script src="/jquery/jquery-1.11.1.min.js"&&/script&
$(document).ready(function(){
$("#btn1").click(function(){
alert("Text: " + $("#test").text());
$("#btn2").click(function(){
alert("HTML: " + $("#test").html());
&p id="test"&这是段落中的&b&粗体&/b&文本。&/p&
&button id="btn1"&显示文本&/button&
&button id="btn2"&显示 HTML&/button&
JavaScript中:
&!DOCTYPE html&
&script src="/jquery/jquery-1.11.1.min.js"&&/script&
function getInnerHTML(){
alert(document.getElementById("btn1").value);
&p id="test"&这是段落中的&b&粗体&/b&文本。&/p&
&button id="btn1"
onclick="getInnerHTML()"
&显示文本&/button&
&button id="btn2"&显示 HTML&/button&
jQuery和JavaScript的区别总结:
*前者click事件是获取元素id进行处理;后者是onclick="getInnerHTML()";
*前者获取元素值时是text();后者是innerText,无();设置值时,前者是text("jing"),后者是innerText="jing";
*注意:两者的设置时,会覆盖原有内容,如果想要插入,使用插入方法。
.html()用为读取和修改元素的HTML标签&&& 对应js中的innerHTML
&.html()是用来读取元素的HTML内容(包括其Html标签),.html()方法使用在多个元素上时,只读取第一个元素
.text()用来读取或修改元素的纯文本内容& 对应js中的innerText
& text()用来读取元素的纯文本内容,包括其后代元素;.text()方法不能使用在表单元素上
.val()用来读取或修改表单元素的value值
&&& .val()是用来读取表单元素的"value"值,.val()只能使用在表单元素上
关于三者的区别
&& 1. .val()方法和.html()相同,如果其应用在多个元素上时,只能读取第一个表单元素的"value"值,但是.text()和他们不一样,如果.text()应用在多个元素上时,将会读取所有选中元素的文本内容。
&&& 2 .html(),.text(),.val()都可以使用回调函数的返回值来动态的改变多个元素的内容。
阅读(...) 评论()如下图,怎么通过js或jquery获取iframe里面body的内容?_百度知道
如下图,怎么通过js或jquery获取iframe里面body的内容?
我有更好的答案
document.getElementById('b').contentW&&&&alert(&win.}&/script&id=&b&&type=&text/javascript&iframe&nbsp&&&&aClick(){var&/head&&body&&input&src=&name=&c&type=&button&&onclick=&aClick()&&id=&&&function&);a&&value=&测试&/&win&=&&lt.txt&&&/body&gt.body.innerText&a;script&nbsp
iframe 里面还有个iframe
,没有id,
name 什么的
没有你加一个就是了呗。 id 和name只是一个标识,完全没影响的。举个例子:老师要叫一个叫张三的起来答问题,如果下面的学生都没有自己名字,他们怎么知道谁是张三呢?
&quot有这种写法吗,你不是只想获取外面一个页面里的内容;#test')}
}););error&){alert(&加载失败;btn1&quot.load(&test1.txt&-----&+&,function(response,status,可以用ajax
$(&#btn1&success&){alert(response + &&).click(function(){
$('button id=&获得外部的内容&/ type=&&button&button&gt,xhr){
if(status==&加载成功!&)}
if(status==&quot
为您推荐:
其他类似问题
iframe的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。为什么js获取的div的内容为undefined。_百度知道
为什么js获取的div的内容为undefined。
在Asp的一个a.aspx中,先在初始化中,调用数据库里的内容并填充到一个div中,然后在cs代码中的一个点击事件中,用js获取这个div中的内容,然后用参数传递的方法传递到另一个aspx中,但js获取div中的内容显示undefined(传递没有问题。)为什么?求解决!!!!...
我有更好的答案
你div的内容就是undefined的。你看一下是否有单词打错了。。下面是一个小例子:&body& &div&id=&div&&style=&width:100&height:100&background:#&display:&&1&/div&&/body&&script& var&oDiv&=&document.getElementById('div'); alert(oDiv.innerHTML);&&&//如果innerHTML打错了,那么alert出来的就是undefined。
用什么方式获取的?jquery的$(&div&).html()或者text()都可以获取值啊
改为:var name=document.getElementById(&name11&).innerHTML;
本回答被提问者和网友采纳
可以说说你是怎么获取的吗,最好能把代码贴上来
为您推荐:
其他类似问题
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。

我要回帖

更多关于 form div 的文章

 

随机推荐