有这么一段源码
<h1 id="blogname">
<a href="http://blog.tom.com/uservalue" id="a"> uservalue的博客 </a>
</h1> 该怎样通过getElementById,substring把"uservalue"提取出来,uservalue是一个变量.
//获取a标签的值
var word = document.getElementById("a").innerText;
// 就是获取【的博客】前面的全部字符串
var par = substring(0,word.length-4);
<h1 id="blogname">
<a href="http://blog.tom.com/uservalue" id="a"> uservalue的博客 </a>
</h1> 该怎样通过getElementById,substring把"uservalue"提取出来,uservalue是一个变量.
//获取a标签的值
var word = document.getElementById("a").innerText;
// 就是获取【的博客】前面的全部字符串
var par = substring(0,word.length-4);
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>create New Form</title>
<script type="text/javascript">
window.onload=function(){
var txt= document.getElementById("blogname").innerText;
alert(txt.match(/^.*?(?=的博客)/g)[0]);
}
</script>
</head>
<body>
<h1 id="blogname"><a href="http://blog.tom.com/uservalue"> uservalue的博客 </a></h1>
</body>
</html>
var word = document.getElementById("a").innerText;
// 就是获取【的博客】前面的全部字符串
var par = substring(0,word.length-4);getElementById("a")的A应改为 blogname才有效!