![](/img/trans.png)
[英]How can I get the value of an attribute inside a <span> element?
[英]Can i get the value of specific CSS attribute inside the element in react?
我是編碼的初學者。
我遇到了在react中可以根據條件動態設置樣式的場景。
style={{display:index > this.state.indexVal ? 'inline': 'none',}}
我可以將上述概念用於獲取當前元素css的一個屬性值並基於該屬性值更改其他元素嗎?
期待這樣的事情
style={{display: currentElement.backgroundColor === 'red ? 'inline': 'none',}}
這不是執行這種檢查並直接訪問元素的反應方式。 由於您想“知道”可以更改的組件狀態(在您的情況下,某些元素的背景色)-您應將其保存為組件狀態(如果從父組件控制它,則應從父元素傳遞給組件props)。 然后,您將可以在狀態或道具中檢查此值。
您可以使用React Refs直接訪問某些元素,但這是一種不好的做法,只有在您100%無法使用props / state做到這一點時,才應該這樣做: https : //reactjs.org/docs/refs-and- the-dom.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.