【发布时间】:2020-10-27 20:03:47
【问题描述】:
希望你一切顺利,并感到安全!
谁能帮我解决这个问题,我正在尝试从 JavaScript 文件读取文件并对其进行操作,这个文件是在 html 代码中实例化的。
我在rhino环境中单独运行JavaScript代码成功了,但是在Firefox/chrome中通过html文件运行时没有成功。
这是 JavaScript 代码:
const paragraph = readFile('file_name');
const found = String(paragraph.match(/set red_range.*/g));
const found_1 = String(paragraph.match(/set orange_range.*/g));
$(document).ready(function(){
var mc = {
red : '',
orange : '',
orange : ''
};
function between(x, min, max) {
return x >= min && x <= max;
}
mc.red = found.split(' ')[2]
mc.orange = found_1.split(' ')[2]
var dc;
var first;
var second;
var th;
$('p').each(function(index){
th = $(this);
dc = parseInt($(this).attr('data-color'),10);
$.each(mc, function(value, name){
first = parseInt(name.split('/')[0],10);
second = parseInt(name.split('/')[1],10);
console.log(between(dc, first, second));
if( between(dc, first, second) ){
th.addClass(value);
}
});
});
});
提前致谢!
【问题讨论】:
-
“但是通过 html 文件运行时我没有”是什么意思?你的意思是你试图在浏览器中运行它?如果有,是什么浏览器?请记住,浏览器不会将用户的文件系统暴露给网页脚本,这对您来说应该是显而易见的原因。
-
是的,我尝试在 Firefox 中运行它,但什么也得不到
-
所以你问为什么那些设计为不能工作的东西不能工作呢?
-
为什么??你能解释一下吗?我是 JavaScript 初学者
-
@Dai,请问有其他解决方案可以完成这项工作吗??
标签: javascript html firefox scripting rhino