javascript获取div的内容 精华篇

 更新时间:2009年05月18日 00:21:32   作者:  
用js获取div的内容的方法,其实原理就是利用innerText或innerHTML
原理:采用innerText 或者 innerHTML
复制代码 代码如下:

<script language=”javascript”>
var stock_code = stockcode.innerText;
var stock_code = stockcode.innerHTML;
</script>
<div id="stockcode" style="display:none">
test
</div>


innerText 跟 innerHTML是两个非DOM标准的方法
其区别如图所示:
(图中应该为innerText)


在IE中 innerText 跟 inner HTML 两个方法都能正常运行

但是FF里面的innerText不可用,但是有一个替代方法: textContent

IE: oDiv.innerText = aString; oDiv.innerHTML = aString;
FF: oDiv.textContent = aString; oDiv.innerHTML = aString;

Ajax in action 的作者之一Eric 用正则表达式 实现了 一个兼容方法,比较有趣
Hope this helps
A 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 = /<\/?[^>]+>/gi;
function ReplaceTags(xStr){
xStr = xStr.replace(regExp,"");
return xStr;
}
</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.

[Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行]

相关文章

  • 基于JavaScript实现表格隔行换色

    基于JavaScript实现表格隔行换色

    这篇文章主要介绍了基于JavaScript实现表格隔行换色,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • js对table的td进行相同内容合并示例详解

    js对table的td进行相同内容合并示例详解

    正如标题所言如何对table的td进行相同内容合并,下面为大家详细介绍下使用js是如何做到的,感兴趣的朋友不要错过
    2013-12-12
  • js实现简单计算器

    js实现简单计算器

    一个简洁的网页JS计算器,附详细代码释义。通过下边的效果演示就可以看到,这是一个挺小的js网页计算器,界面美化的我想还是不错的,毕竟在没有使用任何图片修饰的情况下,很好看,而且功能挺实用,可以完成基本的数学算数运算。
    2015-11-11
  • 类似CSDN图片切换效果脚本

    类似CSDN图片切换效果脚本

    原来的脚本当只有一张图片时会弹出JavaScript脚本错误,特此将自己修改完的版本贴出。
    2009-09-09
  • layui实现tab的添加拒绝重复的方法

    layui实现tab的添加拒绝重复的方法

    今天小编就为大家分享一篇layui实现tab的添加拒绝重复的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-09-09
  • javascript设计模式 接口介绍

    javascript设计模式 接口介绍

    最近在看javascript设计模式的书籍《pro javascript design pattrens》,觉得很不错,可以提高自己对js oo的理解,也可能帮助自己更好的理解别人写的js library,提高自己js的水平
    2012-07-07
  • 浅析如何在Bash中调用Node运行JS文件进行数据通信

    浅析如何在Bash中调用Node运行JS文件进行数据通信

    这篇文章主要来和大家探讨在 Bash 中调用 Node 运行 JS 文件时如何进行数据通信,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-03-03
  • 微信小程序实现计时器开始和结束功能

    微信小程序实现计时器开始和结束功能

    这篇文章主要为大家详细介绍了微信小程序实现计时器开始和结束功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-07-07
  • 深入浅析JavaScript面向对象和原型函数

    深入浅析JavaScript面向对象和原型函数

    这篇文章主要介绍了深入浅析JavaScript面向对象和原型函数的相关资料,需要的朋友可以参考下
    2016-02-02
  • JavaScript中Map和Set数据结构使用方法详解

    JavaScript中Map和Set数据结构使用方法详解

    这篇文章主要介绍了JavaScript中Map和Set数据结构使用方法的相关资料,Map是一种键值对集合,支持任意类型的键和值,保留插入顺序,并提供多种遍历和操作方法,Set是一种唯一值集合,通过哈希算法保证唯一性,提供添加、删除、检查元素的方法,需要的朋友可以参考下
    2025-03-03

最新评论