简体   繁体   English

用样式表中的新样式替换以前的样式值并保存该样式

[英]Replace previous style value with new style in Style sheet and save that style

I'm working on project where we append style of any element using jquery append function and saving that style.我正在处理我们使用 jquery append ZC1C425274E68394F1 和 4Z5074E68394F1 样式的任何元素的 append 样式的项目。 It is creating duplicate row for that style with different value.它正在为该样式创建具有不同值的重复行。 I want to check if some style is exist against this element, class, Id than replace it will new and remove old one.我想检查这个元素是否存在某种样式,class,ID比替换它会新的并删除旧的。 Here is my short code.这是我的短代码。

<input type="number" onclick="changeHeight(this)">
<script> 
    function changeHeight(ele){
        var het=$(ele).val();
        $('style').append("#its_height{height:"+het+px"}");
    }
</script>

MY output :我的 output

<style>
#its_height{color:red}
#its_height{width:40px}
    #its_height{height:30px} #its_height{height:40px} #its_height{height:43px}
</style>

I want that it should be like below this to avoid duplicate enteries and dense data.我希望它应该像下面这样,以避免重复输入和密集数据。 ** it should remove old heigts like 30px and 40px and only show latest height applied to it. ** 它应该删除像30px 和 40px这样的旧高度,并且只显示应用到它的最新高度。 Desired output:所需的 output:

<style>
#its_height{color:red}
#its_height{width:40px}
    #its_height{height:43px}
</style>

Edited Answer: Okay, i see what you are trying to do.编辑后的答案:好的,我知道您要做什么。 Since you are copying the style back to the DB and you already have that covered -- and you simply need to change that portion of the style, use .replace('text to replace', 'new text to use')由于您将样式复制回数据库并且您已经涵盖了该样式 - 您只需更改样式的那部分,请使用.replace('text to replace', 'new text to use')

So first get the computed style and set it in a var, then use that var within the 'text to find' and use your replacement value var within the 'new text to use'.因此,首先获取计算的样式并将其设置在 var 中,然后在“要查找的文本”中使用该 var,并在“要使用的新文本”中使用您的替换值 var。

    function changeHeight(ele){
        var styleToReplace = $('#its_height').css(height),
        het = $(ele).val();
        $('style').html(text.replace(('#its_height{height:')+($.styleToReplace)+('px;}'), ('#its_height{height:')+($.het)+('px;}')));
    }

I'm not sure what the het var is, but the above should replace the current height value with whatever your het var.我不确定het var 是什么,但上面应该用你的 het var 替换当前的高度值。

I'm also not sure why you don't have;我也不确定你为什么没有; after your style properties.在您的样式属性之后。 Shouldn't it be #its_height{height:30px;} with the closing semicolons, or are they not needed here?不应该是带有结束分号的#its_height{height:30px;} ,还是这里不需要它们? I included the semicolons in the above examples anyway.无论如何,我在上面的示例中都包含了分号。

Your last apended height always apply to your element.您最后附加的高度始终适用于您的元素。 If you replace all style so you can use.html, but it replace all other attributes also.如果替换所有样式,则可以使用.html,但它也会替换所有其他属性。

Whatever style you want to change you can keep in separate style tag and provide ID to that tag.无论您想更改什么样式,都可以保存在单独的样式标签中,并为该标签提供 ID。 after then you can change in particular.之后你可以特别改变。 If you will do with style then it will change whole page CSS.如果您将使用样式,那么它将更改整个页面 CSS。

You can follow following example.您可以按照以下示例进行操作。

 function changeHeight(ele){ var het=$(ele).val(); $("style#itsHeightCSS").html(`#its_height{height:${het}px}`) }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> <style> #its_height{ background:#000; width:100px; margin-top:10px; } </style> <style id="itsHeightCSS"> #its_height{height:30px} </style> <input type="number" value="30" onclick="changeHeight(this)"> <div id="its_height"></div>

I would suggest you not to use 'append'.我建议您不要使用“附加”。

Instead, try this,相反,试试这个,

Give a class or ID name to the where you can define all your predefined styles for the input tag in the style section.给一个 class 或 ID 名称,您可以在其中为样式部分中的输入标签定义所有预定义的 styles。

<input id="its_height" type="number" onclick="changeHeight(this)></input>


<style>
    #its_height{
        color:red; 
        width:40px;
        height: 10px; // default height (not mandatory to mention)
    }
</style>

Now in changeHeight method using.css() jquery method update the height of the input section.现在在 changeHeight 方法中使用.css() jquery 方法更新输入部分的高度。

<script>
    function changeHeight(e) {
        var het=$(ele).val();
        var divHeight = $('.col-1').height(); 
        $('#its_height').css('height', het+'px');
    }
</script>

I think this would be helpful for your question.我认为这对您的问题会有所帮助。 Please let me know if this is working out for your case.请让我知道这是否适合您的情况。 We can attain the same solution with the help of vanilla javascript as well.我们也可以借助 vanilla javascript 获得相同的解决方案。 Feel free to post a comment if you are in need of the plain javascript solution.如果您需要简单的 javascript 解决方案,请随时发表评论。

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

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