简体   繁体   English

如何使用JavaScript在我的锚元素上添加样式类名称

[英]How can I add a style class name onto my anchor element using JavaScript

I know how to use JavaScript to add an anchor element to my page, for example 例如,我知道如何使用JavaScript向页面添加锚元素

var userName_a = document.createElement('a');

However, now I'd like to add a style name onto that element also, for example I tried 但是,现在我也想在该元素上添加样式名称,例如,我尝试过

var userName_a = document.createElement('a class="normalLink"');

Which didn't work, and caused my JavaScript to stop running. 哪个无效,并导致我的JavaScript停止运行。

Which is the best way to add a style name to an element Iv'e created using JavaScript? 在使用JavaScript创建的元素中添加样式名称的最佳方法是哪种?

like this 像这样

 var userName_a = document.createElement('a'); document.body.appendChild(userName_a); userName_a.innerHTML='HELLO WORLD'; userName_a.style='color:red;background:yellow' 

The createElement function takes only the name of the HTML tag you wish to create, nothing else. createElement函数仅采用您要创建的HTML标记的名称,而没有其他名称。

You access the style(s) of an element by using style property , or adding/removing style classes either through the className , or classList list 您可以通过使用style属性或通过classNameclassList列表添加/删除样式类来访问元素的样式

Style property 样式属性

userName_a.style.background = "#FF0000";
userName_a.style["font-size"] = "20px";

className 班级名称

userName_a.className = "active";

classList 班级名册

userName_a.classList.add("active");
userName_a.classList.remove("active");
userName_a.classList.toggle("active");

The most consistent way is to use the setAttribute function: 最一致的方法是使用setAttribute函数:

var userName_a = document.createElement('a');
userName_a.setAttribute( 'class', 'normalLink' );
userName_a.setAttribute( 'style', 'color:blue;font-weight:bold;' );

Since it looks like you just want to have a class name on the object, the simplest way to do that is to just assign a class name to the object after you create it: 由于看起来您只想在对象上拥有一个类名,因此最简单的方法是在创建对象后为该对象分配一个类名:

var userName_a = document.createElement('a');
userName_a.className = "normalLink";

You can also just modify the style property directly too: 您也可以直接直接修改style属性:

userName_a.style.color = "red";
userName_a.style.textDecoration = "none";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用.classList.add('class')在具有多个相同class名称的元素与javaScript中的其他元素添加样式 - How to add style on element which have multiple same class name with other elements in javaScript using .classList.add(‘class’) 如何使用jQuery向元素添加与类名称相关的样式? - How to add style relevant to class name to element using jquery? 如何使用 JavaScript 添加类的元素 - How I can add element's of class using JavaScript 如何使用 javascript 添加元素 class? - How can I add an element class with javascript? 如何在 javascript 中添加两个样式元素? - How can I add two style element inside the javascript? 如何将样式元素添加到div并使用javascript将其删除 - How do I add a style element to a div and remove it using javascript 如何在列表元素上添加一个按钮,删除CRUD应用程序的相应元素? - How can I add a button onto my list elements that deletes the respective element for a CRUD app? 如何停止我的JavaScript提供 <nav> 元素也为“被选中”类名 - How can I stop my JavaScript from giving the <nav> element the “is-selected” class name too 如何使用 JavaScript 获取尚不存在的元素的样式? - How can I fetch the style of a not yet existing element using JavaScript? 如何使用Mootools或Javascript激活样式类? - How can I activate a style class on click using Mootools or Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM