简体   繁体   English

jQuery将一个元素的css值设置为另一个

[英]jQuery to set css value of one element to another

I need a little help with setting "padding left" of <div id="menu"> to equal the width of <ol> 我需要将<div id="menu"> “向左填充”设置为等于<ol>的宽度时需要一点帮助

Here's an example code for reference 这是示例代码供参考

<ol style="width:80%">some code</ol>

<div id="menu">some text</div>

I'm trying to do it with jQuery, I don't know it well so this is my attempt 我正在尝试使用jQuery,但我不太了解,所以这是我的尝试

$("#menu").css({"paddingLeft": "$('ol').width()"});

It works only if I enter a value myself like 400px in here $("#menu").css({"paddingLeft": "400px"}); 仅当我在此处$("#menu").css({"paddingLeft": "400px"});输入自己喜欢的像素时,它才有效

But this is for a responsive design and I want it to automaticaly get the value of <ol> element and in pixes 但这是一个响应式设计,我希望它自动获得<ol>元素的值和像素

Remove the " from "$('ol').width()" otherwise it will not work "$('ol').width()"删除" ,否则它将不起作用

$("#menu").css({"paddingLeft": $('ol').width()});

If you want to change the padding while changing the size then put it inside window resize() event 如果要在更改大小时更改填充,则将其放入窗口resize()事件中

$(window).resize(function() {
    $("#menu").css({"paddingLeft": $('ol').width()});
});

很近!

$("#menu").css({"paddingLeft": $('ol').width()});
$("#menu").css({"paddingLeft": $('ol').width()});

From jQuery API 从jQuery API

you can check the method .css( propertyName, value ) and figure it out. 您可以检查方法.css( propertyName, value )并找出.css( propertyName, value )

By the way, when you refresh your window size. 顺便说一句,当您刷新窗口大小时。 code like this: 像这样的代码:

$(window).resize(function (){
   $("#menu").css({"paddingLeft": $('ol').width()});
})

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

相关问题 如何获取一个元素的变化高度值并将其作为 CSS “顶部”值返回 jQuery 中的另一个元素? - How to get a changing height value of one element and return it as a CSS "top" value on another element in jQuery? jQuery / CSS:无法使用另一个元素的offset()值设置元素的位置? - Jquery/CSS: Unable to set position of an element using offset() value of another element? 在每个函数jquery中设置另一个元素值 - Set another element value in each function jquery 将一个标题元素的值设置为另一个 - Set value of one header element to another 使用一个元素的值在jQuery中查找另一个 - using the value of one element to find another in jquery 如何使用CSS / jQuery将一个元素相对于另一个定位 - How to position one element relative another using CSS/jQuery Animate.css jQuery将一个元素动画化 - Animate.css jQuery animate one element after another 将一个属性复制到JQuery中一组子项的同一子项中的另一个属性 - Copy one attribute to another in the same child element of a set of children in JQuery jQuery数据表-将值从一个“数据”元素传递到另一个 - Jquery Datatable - Passing value from one “Data” element to another jQuery根据点击将一个元素的值更改为另一个元素 - Jquery change value of one element to another based on click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM