简体   繁体   English

动态更改CSS属性的正确方法?

[英]Proper way to dynamically change css properties?

I have site that, in response to user interaction, dynamically creates divs using jquery. 我有一个网站,响应用户交互,使用jquery动态创建div。 The div will have a span inside containing a timestamp to show its creation time, and the user can click a button to show or hide the timestamp span. div的内部将包含一个时间戳,以显示其创建时间,并且用户可以单击按钮以显示或隐藏时间戳的跨度。

I ran into the issue of, when the user selects to hide timestamps, how do you prevent future dynamically added spans from showing? 我遇到一个问题,当用户选择隐藏时间戳时,如何防止将来显示动态添加的跨度? In reference to this question Create a CSS rule / class with jQuery at runtime , I added a style tag in the head dynamically. 参考这个问题,在运行时使用jQuery创建CSS规则/类 ,我在头部动态添加了一个样式标签。 However, I also intended to allow the user to be able to choose a font from a drop down list and change the font style of the text inside the divs. 但是,我还打算让用户能够从下拉列表中选择一种字体,并更改div内文本的字体样式。 Following this method now seems like it would create a lot of overhead. 现在采用这种方法似乎会产生很多开销。

Both issues revolve around the same issue: change already existing element and any future dynamically created matching element's css style, but I'm not sure the method mentioned above is really the best solution? 这两个问题都围绕着同一问题:更改现有元素以及将来动态创建的匹配元素的CSS样式,但是我不确定上述方法是否真的是最佳解决方案?

EDIT: SNIPPET 编辑:SNIPPET

 $(function() { $('#add').click(function() { $('#display').append("<div><span class='time'> ex. Timestamp</span> Div text contents...</div>"); }); $('#hidetime').click(function() { $(this).text(function(i, text) { if (text === "Hide Time") { $("<style>").prop("type", "text/css") .html(".time {display: none}").appendTo("head"); return "Show Time"; } else { $('style').remove(); return "Hide Time"; } }); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id='add'>Add</button> <button id='hidetime'>Hide Time</button> <div id='display'> </div> 

You've provided no code to debug, but the way you can do this is to toggle a class such as notimestamps on the container element for the whole thing. 您没有提供要调试的代码,但是您可以执行的方法是在整个容器 notimestamps上切换一个类,例如notimestamps

Then in your main CSS code you can simply do something along the lines of: 然后,在您的主要CSS代码中,您可以按照以下方式简单地执行以下操作:

.container.notimestamps span {
    display:none;
}

If you're changing font styles, you can do something very similar. 如果要更改字体样式,则可以执行类似的操作。

Example: 例:

.container.font-arial {
   font-family: arial, sans-serif;
}
.container.font-tahoma {
   font-family: tahoma, sans-serif;
}

Using your recently added example you would change it to: 使用您最近添加的示例,您可以将其更改为:

 $(function() { $('#add').click(function() { $('#display').append("<div><span class='time'> ex. Timestamp</span> Div text contents...</div>"); }); $('#hidetime').click(function() { $('#display').toggleClass('notimestamp'); }); $('#font').change(function() { $('#display').attr('data-font', $(this).val()); }); }); 
 #display.notimestamp span { display:none; } #display { font-family:sans-serif; } #display[data-font="arial"] { font-family:Arial; } #display[data-font="georgia"] { font-family:Georgia; } #display[data-font="tahoma"] { font-family:Tahoma; } #display[data-font="tnr"] { font-family:"Times New Roman"; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id='add'>Add</button> <button id='hidetime'>Hide Time</button> <select id="font"> <option value="arial">Arial</option> <option value="georgia">Georgia</option> <option value="tahoma">Tahoma</option> <option value="tnr">Times New Roman</option> </select> <div id='display'> </div> 

You can achieve it using plain javascript 您可以使用普通的javascript来实现

Here is an example. 这是一个例子。 You can add any similar styles dynamically 您可以动态添加任何相似的样式

HTML 的HTML

<div id="myDiv" style="margin: 50px 50px 50px 50px">This is a div.</div>
    <br />
<button type="button" onclick="removemargin()">remove margin</button>
<button type="button" onclick="removeLeftMargin()">remove leftmargin</button>
<button type="button" onclick="removeTopMargin()">remove topmargin</button>
<button type="button" onclick="addCssMargin()">add margin by adding class (dominant here)</button>

CSS 的CSS

#myDiv {
    background: #EEE;
}

.myclass{
     margin : 100px 10px 15px 50px !important;
     background:red !important;
}

JAVASCRIPT JAVASCRIPT

function removemargin() {
    document.getElementById("myDiv").style.margin = "0";
}

function removeLeftMargin() {
    document.getElementById("myDiv").style.marginLeft = "0";
}

function removeTopMargin() {
    document.getElementById("myDiv").style.marginTop = "0";
}

function addCssMargin() {
   var d = document.getElementById("myDiv");
   d.className = d.className + " myclass";
}

JsFiddle JsFiddle

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

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