简体   繁体   English

om / react:在渲染目标元素之外操作元素

[英]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.

相关问题 在React组件之外操纵dom元素 - Manipulate dom element outside React component 骨干网:如何在与View关联的元素之外操作元素 - Backbone: how to manipulate elements outside the element associated with View 在React + Redux.js应用程序中,在Provider组件之外操作DOM元素的正确方法是什么? - What is a proper way to manipulate DOM elements outside of Provider component in React + Redux.js app? 根据数据目标操作元素 onclick - Manipulate element onclick base on data-target 使用React,呈现具有多个嵌套元素的元素的正确方法是什么? - Using React, what is the proper way to render an element with multiple nested elements? 更新数组中的1个元素是否会在React中为数组中的其他元素触发渲染? - Does updating 1 element in an array trigger a render in React for other elements in array? React 在单个 Routes 元素中渲染多个 Route 元素 - React Render Multiple Route elements in single Routes element 我应该在 react 元素外使用 div 来通过 jsx 中的 id 获取它并操纵它的样式吗? 反应数据网格 - Should I use a div outside a react element to fetch it by id in jsx and manipulate its style? react-data-grid 根据另一个元素的事件来操作多个元素的React方法是什么? - What's the React way to manipulate multiple elements according to event of another element? 反应:以“ this”的形式访问目标元素 - React: Accessing a target element as 'this'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM