
alert(datainnerHTML);
input 标签有value属性,值是输入框里的内容;
span 标签没有value属性,span内部的内容,用innerHTML属性获得
思路:根据标签名称获取所有span对象,然后根据类名筛选出目标对象,最后使用 innerHTML 属性获取文本。实例演示如下:
1、HTML结构
<span class="xing">姓氏</span><span class="name">名字</span>
<span class="sheng">籍贯</span> <br>
<input type='button' value='获取class=name的span' onClick="fun()"/>
2、javascript代码
function fun(){obj = documentgetElementsByTagName("span");
class_name = "name";
for(i in obj){
if(obj[i]className == class_name){
alert(obj[i]innerHTML);
return
}
}
}
3、效果演示
使用windowgetSelection()getRangeAt(0)toString();
注意window对象,iframe中的选中使用相应的window对象
定位光标的话,有些可以使用focus函数。对于不可编辑的区域要使用Range对象。
然后windowgetSelection()removeAllRanges();//去除现有的光标(选中区域)
windowgetSelection()addRange(Range对象);//添加选中区域选中的范围
第一种方法:输出html
1、<body onload="s()">
2、<span id="hello"></span>
3、<script language="javascript">4、function s()
5、{documentgetElementById("hello")innerHTML = "<iframe src= height=400 width=300></iframe>";}
6、</script>第二种方法:输出文本
1、<body onload="s()">
2、<span id="hello"></span>
3、<script language="javascript">4、function s()
5、{documentgetElementById("hello")innerText = "hello world";}
6、</script>在页面加载完成后通过jquery给多个span赋值
扩展资料
JavaScript一种直译式脚本语言,是一种动态类型、弱类型、基于原型的语言,内置支持类型。它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在HTML(标准通用标记语言下的一个应用)网页上使用,用来给HTML网页增加动态功能。
Javascript脚本语言同其他语言一样,有它自身的基本数据类型,表达式和算术运算符及程序的基本程序框架。Javascript提供了四种基本的数据类型和两种特殊数据类型用来处理数据和文字。而变量提供存放信息的地方,表达式则可以完成较复杂的信息处理。
参考资料:
1、给span一个id,或者给一个特定的class使用jquery的id选择器或者类选择器
<span id="spId" class="content">ajlwe</span>var str=$("#spId")html();--通过id获取span,然后通过html()获取span里的内容var str2=$("#spId")text();--通过id获取span,然后通过text()获取span里的内容
var str3=$("content")html();--通过class获取span,然后通过html()获取span里的内容
var str4=$("content")text();--通过class获取span,然后通过text()获取span里的内容
注:html()返回或设置被选元素的内容 (inner HTML);text()获取设置或返回被选元素的文本内容
有的时候无法取得值时,是因为html是从上往下解析的,在解析到js里的$("#spId")时,下面这个span还不存在,当然就取不到了。
js代码放在<span>下面解析时,span就有了。另外,jQuery的做法是用ready函数包含这些js代码,这样的话,就不存在位置的问题了,放到哪都可以。它的作用就是在加载完整个页面后才执行包含的js
CSS的样式分为三类:
内嵌样式:是写在Tag里面的,内嵌样式只对所有的Tag有效。
内部样式:是写在HTML的里面的,内部样式只对所在的网页有效。
外部样式表:如果很多网页需要用到同样的样式(Styles),将样式(Styles)写在一个以css为后缀的CSS文件里,然后在每个需要用到这 些样式(Styles)的网页里引用这个CSS文件。
getComputedStyle是一个可以获取当前元素所有最终使用的CSS属性值。返回的是一个CSS样式对象([object CSSStyleDeclaration])
currentStyle是IE浏览器的一个属性,返回的是CSS样式对象
element指JS获取的DOM对象
elementstyle //只能获取内嵌样式
elementcurrentStyle //IE浏览器获取非内嵌样式
windowgetComputedStyle(element,伪类) //非IE浏览器获取非内嵌样式
documentdefaultViewgetComputedStyle(element,伪类)//非IE浏览器获取非内嵌样式
注:Gecko 20 (Firefox 4 / Thunderbird 33 / SeaMonkey 21) 之前,第二个参数“伪类”是必需的(如果不是伪类,设置为null),现在可以省略这个参数。
下面的html中包含两种css样式,id为tag的div是内嵌样式,而id为test的div样式为内部样式
<!doctype html><html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="Yvette Lau">
<meta name="keywords" content="样式,对象,内容,内嵌,属性">
<meta name="description" content="alert(datainnerHTML);input 标签有value属性,值是输入框里的内容;span 标签没有value属性,span内部的内容,用innerHTML属性获得思路:根据标签名称获取所有span对象,然后根据类名筛选出目标">
<title>Document</title>
<style>
#test{
width:500px;
height:300px;
background-color:#CCC;
float:left;
}
</style>
</head>
<body>
<div id = "test"></div>
<div id = "tag" style = "width:500px; height:300px;background-color:pink;"></div>
</body>
</html><script type = "text/javascript">
windowonload = function(){
var test = documentgetElementById("test");
var tag = documentgetElementById("tag");
//CSS样式对象:CSS2Properties{},CSSStyleDeclaration
consolelog(teststyle); //火狐返回空对象CSS2Properties{},谷歌返回空对象CSSStyleDeclaration{}
consolelog(tagstyle); //返回CSS2Properties{width:"500px",height:"300px",background-color:"pink"}
//elementstyle获取的是内嵌式的style,如果不是内嵌式,则是一个空对象
consolelog(tagstylebackgroundColor);//pink
consolelog(tagstyle['background-color']);//pink
//获取类似background-color,border-radius,padding-left类似样式的两种写法啊
consolelog(testcurrentStyle) //火狐和谷歌为Undefined,IE返回CSS对象
consolelog(windowgetComputedStyle(test,null))//谷歌返回CSSStyleDeclaration{……} ,火狐返回CSS2Properties{……}
consolelog(windowgetComputedStyle(test))
//效果同上,但是在Gecko 20 (Firefox 4/Thunderbird 33/SeaMonkey 21) 之前,第二个参数“伪类”是必需的(如果不是伪类,设置为null)
consolelog(testcurrentStylewidth);//500px(IE)
consolelog(windowgetComputedStyle(test)width); //500px;
consolelog(windowgetComputedStyle(test)['width']);//500px;
//documentdefaultViewgetComputedStyle(element,null)[attr]/windowgetComputedStyle(element,null)[attr]
}
</script>
function test()
{
var spans=documentgetElementsByTagName("span");
var arr=[];
for(var i=0;i<spanslength;i++)
{
if(spans[i]id=="")
{arrpush("没有id属性");}
else
{arrpush(spans[i]id);}
}
alert(arrjoin("[+]"));
}
</script>
以上就是关于在一个JSP页面中使用javascript获得<span>标签里的内容全部的内容,包括:在一个JSP页面中使用javascript获得<span>标签里的内容、js怎么获取<span class="name">名字</span>中的内容:名字、js如何获得当前li下的span文本等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)