Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to do the following:(我要执行以下操作:)

<div id="theDiv" style="width: aJavascriptVariableOrFunctionCallToGetValue">TESING</div> I don't want to use, elsewhere in the code,(我不想在代码的其他地方使用) document.getElementById('theDiv').style.width = someValue; I actually want that div, when it first appears , to have a width set, inline, by either a JavaScript variable or by way of a call to a JavaScript function.(实际上,我实际上希望该div 在首次出现时通过JavaScript变量通过调用JavaScript函数的方式内联设置宽度。) How can I do this?(我怎样才能做到这一点?)   ask by CFHcoder translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
141 views
Welcome To Ask or Share your Answers For Others

1 Answer

This is impossible to do the way you see this.(这是不可能做到的。)

Every time the variable changes, you need to update the style of that particular object:(每当变量更改时,您需要更新该特定对象的样式:) var theDiv = document.getElementById("theDiv"); document.getElementById('theDiv').style.width = someValue; I really don't understand what you mean that when it first appears you want it's width to be set to certain width - why do you want to do that inline?(我真的不明白您的意思是,当它第一次出现时,您希望将其宽度设置为一定的宽度-为什么要内联?) Why can't you just set the width in your Javascript?(为什么不能只在Javascript中设置宽度?) What's preventing you from doing that?(是什么阻止您这样做?) Especially if you want to do it just once and don't want to change it dynamically.(尤其是如果您只想执行一次并且不想动态更改它。) If you want to link the width of the div to a variable, look at frameworks like Backbone or EmberJS.(如果要将div的宽度链接到变量,请查看Backbone或EmberJS之类的框架。) You can then define a renderer that changes the width when the variable changes.(然后,您可以定义在变量更改时更改宽度的渲染器。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...