可以直接用name这个属性取到的name的值。下面是个小例子。仅供参考:
<body>
<div id="div" name="div" style="width:100px; height:100px; background:#ccc;"></div>
</body>
<script>
var oDiv = documentgetElmentById('div');
oDivonclick=function(){
alert(oDivname); //这个时候填出 div
};
</script>
var studentList = [{
name: "a",
age: 17
}, {
name: "b",
age: 18
}, {
name: "c",
age: 19
}, {
name: "d",
age: 20
}, {
name: "e",
age: 18
}];
function displayStudentType(nianlin) {
for (var i = 0; i < studentListlength; i++) {
var si = studentList[i];
if (siage == nianlin) {
consolelog(siname);
}
}
}
displayStudentType("17");
displayStudentType("18");
代码如下:
这细微的差别就会影响到程序的结果。
代码如下:
这些颜色不仅作用于那些直接包含在<body>标签里的内容,还将作用于嵌套在body元素内的所有元素。
id属性就像一个挂钩,它一头连着文档里的某个元素,另一头连着CSS样式表里的某个样式。
documentgetElementById("purchases")这个调用将返回一个对象,这个对象对应着document对象里的一个独一无二的元素,那个元素的的id属性值为purchases。其实,文档中的每个元素都是一个对象。利用DOM提供的方法能够得到任何一个对象。getElementsByTagName返回一个数组,即使整个文档里只有一个元素,也返回一个数组。
示例:
显示信息都是object,getElementByClassName也返回一个具有相同类名的元素数组,获取和设置属性getAttribute objectgetAttribute(attribute)。
注:getAttribute方法不属于document对象,它只能通过元素节点对象调用。
例子:
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="描述">
<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>
以上就是关于javascript怎么获得元素的name属性全部的内容,包括:javascript怎么获得元素的name属性、javascript 如何获取所有有某一相同属性的元素、js中如何获取某一个属性的值,如何设置一个属性的值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)