使用指令在camelCase中设置元素的属性值 [英] Setting attribute value of an element in camelCase using a directive

查看:496
本文介绍了使用指令在camelCase中设置元素的属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  element.attr(' startOffset',val); 

但是当我检查元素时,添加的属性是'startoffset',其中'o'不是一个大写字母。



有没有办法给元素添加一个属性并保持这个单词的大小写?


$ b $如果你使用jqliteWrapper .attr

解决方案 c>甚至直接使用DOM操作 .setAttribute ,它会在属性名称附加到元素之前小写。


在HTML文档中的HTML元素上调用setAttribute时,它的属性名称参数会变小。


由于您正在使用SVG,请尝试使用 setAttribute ,但设置属性保留SVG的情况下,不确定是否jQuery内部进行任何转换。

  element [0] .setAttribute('startOffset',val); 

Plnkr






证实它是jquery列入在使用angular之前,它不会保留属性名称外壳,而通过 .attr 来设置它,但是如果您不包含jquery并且角度会回退到jqLit​​e,它将设置属性名称是这样的,所以除了直接的DOM操作之外,还可以和SVG一起工作(与 attrs。$ set attrs。$ set )。


I'm trying to add an attribute to an angularJs element from a directive like this:

element.attr('startOffset', val);

But when I check the element, the attribute added is 'startoffset' where the 'o' is not a capital letter.

Is there any way to add an attribute to an element and keep the case of the word intact?

Thanks

解决方案

If you set an attribute using jqliteWrapper .attr or even with direct DOM operation .setAttribute it will lowercase the attribute name, before attaching to the element.

When called on an HTML element in an HTML document, setAttribute lower-cases its attribute name argument.

Since you are using SVG try direct operation with setAttribute, however setting attributes preserve their cases with SVG, not sure if jquery does any transformations internally.

 element[0].setAttribute('startOffset', val);

Plnkr


Confirmed that it is jquery inclusion before angular which causes it not to preserve the attribute name casing while setting it via .attr, but if you do not include jquery and angular falls back to jqLite it will set the attribute name as is, so that will work (along with attrs.$set) with SVG as well apart from the direct DOM operation.

这篇关于使用指令在camelCase中设置元素的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆