`
scomouse
  • 浏览: 14827 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

attr函数

阅读更多

attr 函数 就是给元素添加属性

属性有2中

元素本身就有的 如 name id =======

还有就是用户自定义属性 如 vp

然后ie下的属性设置 跟 其他的浏览器有些区别 简单记录一下

设置元素的style

ie6,7下面

元素不能直接设置style这属性

必须用元素样式的cssText 来设置值

一个例子

<a href="#" id=a2">123123</a>

ie 6,下面

a.style.setAttribute('cssText',"color:#f00")

非ie6,7下面

a.setAttribute('style',"color:#f00")

修改input的type

ie 6,7,8下面(9没有测试) 已经添加到dom树上的input是不允许修改type的

非ie可以

一些元素的属性 如 href|src|style

在ie下面 写相对路径的时候取值 会出现绝对路径

有种用法

a.getAttribute('href',2)

第2个参数为0,1,2

参考这个文章

还有透明度 不一样

上面瞎篡改代码

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>attr</title>
<link href="manage.css" mce_href="manage.css" rel="stylesheet" type="text/css" />
</head>

<a id="a2">sssssss</a>
<div id="a1"></div>
<body>
<script type="text/javascript">
(function(){
var div = document.createElement("div");
div.innerHTML = ' <link/><table></table><a href="/a" mce_href="a" style="color:red;float:left;opacity:.5;" mce_style="color:red;float:left;opacity:.5;">a</a><select><option>text</option></select><object><param/></object>';
var a = div.getElementsByTagName("a")[0];
window.support = {
style : /red/.test(a.getAttribute("style")),
opacity : a.style.opacity === "0.5",
hrefNormalized : a.getAttribute("href") === "/a"
};
})();

function attr(elem, name, value){
if (!elem || elem.nodeType == 3 || elem.nodeType == 8)
return undefined;

var set = value !== undefined;

//如果elem是元素
if(elem.tagName){
//这几个元素在ie下面要进行特殊的处理
var special = /href|src|style/.test(name);

//if (name == "selected" && elem.parentNode)
//elem.parentNode.selectedIndex;

//如果是元素原有的属性
if (name in elem && !special){
//存在 value值的情况
if (set){
//ie下面无法修已经存在的input 的 type
if ( name == "type" && elem.nodeName==="INPUT" && elem.parentNode )
throw "type property can't be changed";

elem[ name ] = value;
}

//如果不存在value 这个时候是获取这个值了
if( elem.nodeName==="FROM" && elem.getAttributeNode(name) )
return elem.getAttributeNode(name ).nodeValue;

return elem[name];
}

//在ie6,7下面不能直接用attribute来操作样式 所以用cssText
if (!support.style && name == "style")
return arguments.callee( elem.style, "cssText", value );

//如果不是元素原有的属性 且有值
if(set)
elem.setAttribute(name, "" + value);

//href|src|style 设置属性得用elem.getAttribute( name,2)
var attr = !support.hrefNormalized && special
// Some attributes require a special call on IE
? elem.getAttribute( name,2)
: elem.getAttribute( name );

return attr === null ? undefined : attr;
}

if (!support.opacity && name == "opacity" ) {
if (set) {
elem.zoom = 1;
elem.filter=(elem.filter || "").replace( /alpha\([^)]*\)/, "" )+ (parseInt( value ) + '' == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")");

}

return elem.filter && elem.filter.indexOf("opacity=") >= 0 ?
(parseFloat( elem.filter.match(/opacity=([^)]*)/)[1] ) / 100) + '':
"";
}

//这个后面的主要应用于样式 elem.style
name = name.replace(/-([a-z])/ig, function(all, letter){
return letter.toUpperCase();
});

if (set)
elem[ name ] = value;

return elem[name];
}

window.onload = function(){
var div = document.getElementById('a1'),
a = document.getElementById('a2');
attr(div,'n',12312323);
attr(div,'style','height:200px;width200px');
attr(div.style,'background-color','#ff0000');
attr(div.style,'opacity',0.2);
attr(a,'href','http://www.baidu.com');
}
</script>
</body>
</html>

版权声明:本文为博主原创文章,未经博主允许不得转载。

分享到:
评论

相关推荐

    Python中getattr函数和hasattr函数作用详解

    主要介绍了Python中getattr函数和hasattr函数作用的相关知识,非常不错具有参考借鉴价值,需要的朋友可以参考下

    attr() 获取多个a标签 ,并根据不同的id 调用不同的function()

    attr() 函数获取html格式下多个标签,并且根据不同id 调用不同的function()函数 给标签添加id ,根据不同id 获取属性病赋值

    wordpress esc_attr()函数

    esc_attr()函数主要用于对字符串进行转义,比如 “” , “,”,“ ” ” 【函数使用】 【参数说明】 $text (string) (必须) 需要过滤的字符串. 默认: None 【返回值】 (string) 过滤掉特色字符的字符串。 【函数...

    attr:简单的装饰器以DRY方式设置目标函数或类的属性

    简单的装饰器,以DRY方式设置目标函数或类的属性。 用法示例: # Django proposes: def my_calculated_field(... my_calculated_field.short_description = 'Field' my_calculated_field.admin_order_field = '...

    jQuery中attr()与prop()函数用法实例详解(附用法区别)

    本文实例讲述了jQuery中attr()与prop()函数用法。分享给大家供大家参考,具体如下: 一、jQuery的attr()方法 jquery中用attr()方法来获取和设置元素属性,attr是attribute(属性)的缩写,在jQuery DOM操作中会经常...

    attr_extras, 使用attr_initialize之类的方法从 ruby 中取出一些样板.zip

    attr_extras, 使用attr_initialize之类的方法从 ruby 中取出一些样板 attr_extras从 ruby 中提取一些样板,降低了提取小型聚焦类的障碍,而不考虑使用 Struct 函数的缺点。代替class InvoiceBuilder def initialize...

    python中根据字符串调用函数的实现方法

    在python中可以根据字符串来调用函数: 1、使用getattr从字符串来...[root@python 530]# cat attr.py #!/usr/bin/env python def kel(): print 'this is a kel function' def smile(): print 'this is a smile func

    C语言线程调度与优先级配置示例

    通过配置线程调度策略与优先级,可使各线程按照设定的顺序执行,从而达到线程间同步的目的,线程属性配置函数包括: pthread_attr_init(&attr[i]);// 初始化线程属性 pthread_attr_setschedpolicy(&attr[i], SCHED...

    深入理解React中何时使用箭头函数

    相信大家当想起箭头函数时,脑海里可能会浮现 棒,酷,简洁,有趣 等形容词,其实,我们存在一些 更充分的理由 使我们在联想起 箭头函数 时不得不想到的,本文详细的给大家介绍了关于React何时使用箭头函数的相关...

    关于C语言多线程pthread库的相关函数说明

    线程相关操作说明 一 pthread_t pthread_t在头文件/usr/include/bits/...extern int pthread_create __P ((pthread_t *__thread, __const pthread_attr_t *__attr, void *(*__start_routine) (void *), void *__arg

    WordPress中调试缩略图的相关PHP函数使用解析

    the_post_thumbnail 函数的使用 the_post_thumbnail( $size , $attr) 函数参数 $size 是指你想要的缩略图类型 默认是 ‘post-thumbnail’ 也就是特色图像 $attr 图像img标签中的属性设置。 the_post_thumbnail ...

    d3.js中冷门却实用的内置函数总结

    其实在d3.js的API文档中,提供了很多内置的函数,但是有些很有用的内置函数却被无情的忽略了,这篇博客就来整理整理这些被遗忘却很实用的函数。希望整理过后,他们不再被遗忘。 selection.classed(name,【value】) ...

    wordpress form_option()函数

    【函数介绍】 通过esc_attr()过滤option选项值. 【函数使用】 【参数说明】 $option (string) (必须) 选项名称. Default: None 【返回值】 (Echo) 输出通过esc_attr()处理后的wordpress get_option()的值 【说明...

    PostgreSQL中文手册9.2

    三、 pg_attrdef: pg_attrdef: pg_attrdef:pg_attrdef:pg_attrdef:pg_attrdef:pg_attrdef:pg_attrdef:pg_attrdef: . 63 四、 pg_authid: pg_authid: pg_authid: pg_authid:pg_authid: 64 五、 pg_auth_members: pg_...

    jquery1.4 教程一 便利的设置函数

    便利的设置函数 jquery1.4的大部分设置值对方法都可以设置第二个参数为回调函数。比如:.css(), .attr(), .val(), .html(), .text()。 这说法很抽象,来看示例 http://demo.jb51.net/js/jquery1.4/jquery_1.htm ...

    关于原生js的几个工具函数和方法

    attr: 动画方位和如何动画 target: 移动边界 speed: 移动速度 function: 回调函数(增加动画无限可能) ) obj.timer来存储元素自己的定时器标识 3、实现获取某个dom元素某个样式属性(处理兼容)

    jQuery使用元素属性attr赋值详解

    本文主要给大家讲解的是jQuery使用元素属性attr设置多个键值或函数的方法和示例,非常的实用,推荐给小伙伴们参考下。

    基于pthread_create,readlink,getpid等函数的学习与总结

    pthread_create是UNIX环境创建线程函数 具体格式: #include&lt;pthread&gt; int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict attr,void*(*start_rtn)(void*),void *restrict arg);...

    jquery 获取自定义属性(attr和prop)的实现代码

    不提供值,而是提供一个函数,由这个函数计算的值作为属性值。) 4.attr(properties) //给指定元素设置多个属性值,即:{属性名一: “属性值一” , 属性名二: “属性值二” , … … }。(这是一种在所有匹配

Global site tag (gtag.js) - Google Analytics