[英]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()});
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.