[英]om/react: manipulate elements outside the render target element
I'm using om as a clojurescript react interface. 我使用om作为clojurescript react接口。
One question, which I guess relates to both om and react: 我猜这与om和反应有关的一个问题:
Inside my html body I have a div of the id "app", which is used for om/react as a render target. 在我的html主体中,我有一个id为“ app”的div,用于om / react作为渲染目标。
What would be a prefered way to change attributes outside of this element. 在此元素之外更改属性的首选方法是。 more concretely I need to set some stylesheets to the body.
更具体地说,我需要为主体设置一些样式表。
Now, more clojure specific: 现在,更多特定于clojure的内容:
How do you set multiple key-value pairs to a javascript object. 如何将多个键值对设置为javascript对象。 (eg document.body.style)
(例如document.body.style)
I'm using this: 我正在使用这个:
(doseq [[k v] {"backgroundColor" "red" "overflow" "hidden" ...}]
(aset js/document.body.style k v))
There was a nice way to do so with underscore.js: 使用underscore.js有一个很好的方法:
_.extend(document.body.style, {"backgroundColor": "red" "overflow": "hidden"})
Well , but this was the question here. 好吧 ,但这是这里的问题。 Maybe it's not really needed because there is a special om/react way to go.
也许并不需要,因为有一种特殊的om / react方法。
更好的方法是简单地使用包含所有键值对的javascript对象设置主体样式:
(set! (.. js/document -body -style) #js {:backgroundColor "red" :overflow "hidden"})
The solution provided by Naomi is great, but it uses the bad practice of inline css. Naomi提供的解决方案很棒,但是它使用了内联CSS的错误做法。 Instead of setting the actual css styles in code, I would set a class to the desired html object, and in the styles sheets define the css properties of that class.
我不用在代码中设置实际的CSS样式,而是将一个类设置为所需的html对象,然后在样式表中定义该类的CSS属性。
For example: 例如:
(set! (.. js/document -body -className) "my-class")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.