+ -
当前位置:首页 → 问答吧 → 怎么取出字符串中body中的html内容和body中的属性?

怎么取出字符串中body中的html内容和body中的属性?

时间:2011-12-01

来源:互联网

字符串如下:
JScript code

var str='<body bgColor="#f98405" background="" style="font-size:10pt;font-family:NSimSun;margin:2px;"><div>正文</div></body>'

作者: 6rl   发布时间: 2011-12-01

这个应该要用正则。。坐等正则高手

作者: zsx841021   发布时间: 2011-12-01

这是示例

window.onload = function () {
  var rg = new RegExp("^<body([^<]*)>(.*)</body>$", "i"), 
  rgAtt = new RegExp("([a-z]+)\\s*=\\s*([\"'])([^\"']*)\\2", "i");
  var str = '<body bgColor="#f98405" background="" style="font-size:10pt;font-family:NSimSun;margin:2px;"><div>正文</div></body>';
  var match = rg.exec(str);
  if (match && match.length == 3) {
  //ok
  var attStr = match[1], 
  contentHtml = match[2], 
  divC = document.getElementById("c");
  divC.appendChild(document.createTextNode("内容:\n" + contentHtml + "\n\n属性:\n"));
  while (rgAtt.test(attStr)) {
  divC.appendChild(document.createTextNode(RegExp.$1 + " : " + RegExp.$3 + "\n"));
  attStr = RegExp.rightContext;
  }

  }
}

作者: ohmysee   发布时间: 2011-12-01

内容如果比较少,可以用正则,但如果内容较多,情况比较复杂,就应该考虑采集时的方式。好的方法是直接在采集时利用DOM关系进行采摘。给个提示,自己去琢磨吧。

作者: theforever   发布时间: 2011-12-01

点击查看示例
JScript code
window.onload = function () {
  var rg = new RegExp("^<body([^<]*)>(.*)</body>$", "i"),  
  rgAtt = new RegExp("([a-z]+)\\s*=\\s*([\"'])([^\"']*)\\2", "i");
  var str = '<body bgColor="#f98405" background="" style="font-size:10pt;font-family:NSimSun;margin:2px;"><div>正文</div></body>';
  var match = rg.exec(str);
  if (match && match.length == 3) {
  //ok
  var attStr = match[1],  
  contentHtml = match[2],  
  divC = document.getElementById("c");
  divC.appendChild(document.createTextNode("内容:\n" + contentHtml + "\n\n属性:\n"));
  while (rgAtt.test(attStr)) {
  divC.appendChild(document.createTextNode(RegExp.$1 + " : " + RegExp.$3 + "\n"));
  attStr = RegExp.rightContext;
  }
  }
}

作者: ohmysee   发布时间: 2011-12-01

具体还要看需要啊,如果只考虑较新的浏览器,IE9 Chrome FireFox等的话利用document.body.attributes集合可以获取设置过的属性集合。但是IE8,IE7就蛋疼了,它们会把所用支持的属性都放在里面不管你有没有设置。

作者: ohmysee   发布时间: 2011-12-01

HTML code
<script>
    var str='<body bgColor="#f98405" background="" style="font-size:10pt;font-family:NSimSun;margin:2px;"><div>正文</div></body>'
    var mats = str.match(/<body([^>]*)>(.*?)<\/body>/);
    var text = mats[2];
    var str_attr = mats[1];
    var reg = /(\w+)\s*=\s*(["']?)(.*?)\2/g, tmp, attrs = {};
    while(tmp =reg.exec(str_attr)) {
        attrs[tmp[1]] = tmp[3];
    }
    console.log(text);
    console.log(attrs);
    console.log(attrs.bgColor);
</script>

作者: sohighthesky   发布时间: 2011-12-01

接上
实际上现在关于样式的标签属性都由CSS接管了。
像body这样元素除了设置cssStyle的style和class属性外没什么其它属性需要设置。
超链 a 常用属性:href target title style class
图片 img 常用属性:src alt title style class
等等.....

作者: ohmysee   发布时间: 2011-12-01

引用 5 楼 ohmysee 的回复:

具体还要看需要啊,如果只考虑较新的浏览器,IE9 Chrome FireFox等的话利用document.body.attributes集合可以获取设置过的属性集合。但是IE8,IE7就蛋疼了,它们会把所用支持的属性都放在里面不管你有没有设置。

while(tmp =reg.exec(str_attr)) {
  attrs[tmp[1]] = tmp[3];
}
这一段有点问题,exec不会移到下次匹配位置的呀???

JScript code

//应该这样啊
while(tmp =reg.exec(str_attr)) {
    attrs[tmp[1]] = tmp[3];
    str_attr=RegExp.rightContext;
}

作者: ohmysee   发布时间: 2011-12-01

热门下载

更多