Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从javascript代码中读取内部的类属性_Javascript_Vue.js - Fatal编程技术网

从javascript代码中读取内部的类属性

从javascript代码中读取内部的类属性,javascript,vue.js,Javascript,Vue.js,是否可以从vue中的脚本部分读取样式部分的类值 e、 g我有: .node-output02 { bottom: #{-2+$portSize/-2}px; left: #{$nodeWidth/3}px; } 在脚本中,我要执行以下操作: const left = .node-output02.left 这不是一个Vue问题,而是一个一般的JS问题 使用(用于从样式表获取CSS文本),可以将CSS解析为可查询的对象 这不是傻瓜式的,但这是一个开始 console.lo

是否可以从vue中的脚本部分读取样式部分的类值

e、 g我有:

.node-output02 {
    bottom: #{-2+$portSize/-2}px;
    left: #{$nodeWidth/3}px;
  }
在脚本中,我要执行以下操作:

const left = .node-output02.left

这不是一个Vue问题,而是一个一般的JS问题

使用(用于从样式表获取CSS文本),可以将CSS解析为可查询的对象

这不是傻瓜式的,但这是一个开始

console.log('left=',getStyleObject('.node-output02')['left']);//420px
函数getStyleText(类名){
让类=document.styleSheets[0]。规则| | document.styleSheets[0]。cssRules,
styleText='';
Array.from(class).forEach(clazz=>{
if(clazz.selectorText==className){
styleText+=clazz.cssText | | clazz.style.cssText;
}
});
返回样式文本;
}
函数getStyleObject(类名){
让styleText=getStyleText(类名);
如果(styleText.length>0){
让leftBraceIndex=styleText.indexOf('{'),
rightBraceIndex=styleText.lastIndexOf('}'),
选择器=styleText.substring(0,leftBraceIndex).trim(),
ruleText=styleText.substring(leftBraceIndex+1,rightBraceIndex).trim();
返回对象.fromEntries(ruleText.split(/\s*;\s*/g)
.filter(v=>v.length>0)//过滤空(如果有尾随分号)
.map(v=>v.split(/\s*:\s*/g));
}
返回null;
}
.node-output2{
底部:360px;
左:420px;

}
这不是一个Vue问题,而是一个一般的JS问题

使用(用于从样式表获取CSS文本),可以将CSS解析为可查询的对象

这不是傻瓜式的,但这是一个开始

console.log('left=',getStyleObject('.node-output02')['left']);//420px
函数getStyleText(类名){
让类=document.styleSheets[0]。规则| | document.styleSheets[0]。cssRules,
styleText='';
Array.from(class).forEach(clazz=>{
if(clazz.selectorText==className){
styleText+=clazz.cssText | | clazz.style.cssText;
}
});
返回样式文本;
}
函数getStyleObject(类名){
让styleText=getStyleText(类名);
如果(styleText.length>0){
让leftBraceIndex=styleText.indexOf('{'),
rightBraceIndex=styleText.lastIndexOf('}'),
选择器=styleText.substring(0,leftBraceIndex).trim(),
ruleText=styleText.substring(leftBraceIndex+1,rightBraceIndex).trim();
返回对象.fromEntries(ruleText.split(/\s*;\s*/g)
.filter(v=>v.length>0)//过滤空(如果有尾随分号)
.map(v=>v.split(/\s*:\s*/g));
}
返回null;
}
.node-output2{
底部:360px;
左:420px;

}
欢迎来到SO。你能分享你迄今为止在研究中尝试或发现的代码吗?这能回答你的问题吗?这有点倒退。首先用Vue设置样式,这样以后就不需要从CSS访问它了,因为您已经拥有了这些值。Polywhill先生提供的解决方案令人惊叹。谢谢你们!欢迎来到SO。你能分享你迄今为止在研究中尝试或发现的代码吗?这能回答你的问题吗?这有点倒退。首先用Vue设置样式,这样以后就不需要从CSS访问它了,因为您已经拥有了这些值。Polywhill先生提供的解决方案令人惊叹。谢谢你们!非常感谢你。这是完美的解决方案。非常感谢。这是一个完美的解决方案。