简体   繁体   English

使用 Javascript 添加内联样式

[英]Add inline style using Javascript

I'm attempting to add this code to a dynamically created div element我正在尝试将此代码添加到动态创建的 div 元素

style = "width:330px;float:left;" 

The code in which creates the dynamic div is创建动态div的代码是

var nFilter = document.createElement('div');
nFilter.className = 'well';
nFilter.innerHTML = '<label>' + sSearchStr + '</label>';

My idea is to add the style after < div class="well" but i don't know how i'd do it.我的想法是在< div class="well"之后添加样式,但我不知道该怎么做。

nFilter.style.width = '330px';
nFilter.style.float = 'left';

This should add an inline style to the element.这应该为元素添加内联样式。

You can do it directly on the style:您可以直接在样式上执行此操作:

var nFilter = document.createElement('div');
nFilter.className = 'well';
nFilter.innerHTML = '<label>'+sSearchStr+'</label>';

// Css styling
nFilter.style.width = "330px";
nFilter.style.float = "left";

// or
nFilter.setAttribute("style", "width:330px;float:left;");

Using jQuery :使用 jQuery :

$(nFilter).attr("style","whatever");

Otherwise :否则 :

nFilter.setAttribute("style", "whatever");

should work应该管用

You can try with this你可以试试这个

nFilter.style.cssText = 'width:330px;float:left;';

That should do it for you.那应该为你做。

var div = document.createElement('div');
div.setAttribute('style', 'width:330px; float:left');
div.setAttribute('class', 'well');
var label = document.createElement('label');
label.innerHTML = 'YOUR TEXT HERE';
div.appendChild(label);

A few people have an example using setAttribute which I like.一些人有一个使用我喜欢的 setAttribute 的例子。 However it assumes you don't have any styles currently set.但是,它假定您当前没有设置任何样式。 I would maybe do something like:我可能会做这样的事情:

nFilter.setAttribute('style', nFilter.getAttribute('style') + ';width:330px;float:left;');

Or make it into a helper function like this:或者把它变成这样的辅助函数:

function setStyle(el, css){
  el.setAttribute('style', el.getAttribute('style') + ';' + css);
}

setStyle(nFilter, 'width:330px;float:left;');

This makes sure that you can add styles to it continuously and it won't remove any style currently set by always appending to the current styles.这确保您可以连续添加样式,并且不会通过始终附加到当前样式来删除当前设置的任何样式。 It also adds an extra semi colon so that if there is a style ever missing one it will append another to make sure it is fully delimited.它还添加了一个额外的分号,以便如果某个样式丢失了一个,它将附加另一个以确保它被完全分隔。

你应该创建一个 css 类.my_style然后使用.addClass('.mystyle')

If you don't want to add each css property line by line, you can do something like this:如果不想逐行添加每个 css 属性,可以执行以下操作:

 document.body.insertAdjacentHTML('afterbegin','<div id="div"></div>'); /** * Add styles to DOM element * @element DOM element * @styles object with css styles */ function addStyles(element,styles){ for(id in styles){ element.style[id] = styles[id]; } } // usage var nFilter = document.getElementById('div'); var styles = { color: "red" ,width: "100px" ,height: "100px" ,display: "block" ,border: "1px solid blue" } addStyles(nFilter,styles);

使用cssText

nFilter.style.cssText +=';width:330px;float:left;';

尝试这样的事情

document.getElementById("vid-holder").style.width=300 + "px";

Do it with css now that you've created the class. 现在你已经创建了这个类,用css做。 .well { width:330px; .well {宽度:330px; float:left; 向左飘浮; } }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM