Javascript 为什么可以';我们不检查react本机应用程序的样式属性吗?
我想检查一个元素的颜色是否为白色,如下所示Javascript 为什么可以';我们不检查react本机应用程序的样式属性吗?,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我想检查一个元素的颜色是否为白色,如下所示 if(styles.background=='white') console.log("ok") console.log(styles.background=='white') --> was false [1] 为什么[1]返回false?在您的示例中,样式是样式表对象 您需要使用样式表。展平功能如下: const styles = StyleSheet.create({ container: { flex: 1,
if(styles.background=='white')
console.log("ok")
console.log(styles.background=='white') --> was false [1]
为什么[1]返回false?在您的示例中,样式是样式表对象 您需要使用样式表。展平功能如下:
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF'
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
var styleObj = StyleSheet.flatten([styles.container])
console.warn(styleObj.backgroundColor==='#F5FCFF') //=>true
let backgroundColor = Stylesheet.flatten(this.props.style).backgroundColor;
要处理组件的道具样式,可以按如下方式使用它:
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF'
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
var styleObj = StyleSheet.flatten([styles.container])
console.warn(styleObj.backgroundColor==='#F5FCFF') //=>true
let backgroundColor = Stylesheet.flatten(this.props.style).backgroundColor;
您可以在此处找到该函数的源代码:
资料来源及详情如下:
只需确保参数传递语法正确。
(注意:参数周围不需要方括号。) 对于单表单样式表:
var styleObj = StyleSheet.flatten(styles.container)
var styleObj = StyleSheet.flatten(styles[1].container)
对于多表单样式表:
var styleObj = StyleSheet.flatten(styles.container)
var styleObj = StyleSheet.flatten(styles[1].container)
然后,您可以将其打印为dict以检查属性:
console.log(styleObj)
显示完整文件。
console.log(styles.background)
?返回的是什么false@DinukaSalwathura哪一个是假的console.log(styles.background=='white')
或console.log(styles.background')
?如果继承了样式,我相信它不会显示在元素样式下。仅当它在元素上显式设置时。如果您有访问权限,或许可以尝试使用getComputedStyle
。