我试着打开文件
window.open("file:///D:/Hello.txt");
Run Code Online (Sandbox Code Playgroud)
浏览器不允许以这种方式打开本地文件,可能是出于安全原因.我想在客户端使用该文件的数据.如何在Javascript中读取本地文件?
此URL返回JSON:
{
query: {
count: 1,
created: "2015-12-09T17:12:09Z",
lang: "en-US",
diagnostics: {},
...
}
}
Run Code Online (Sandbox Code Playgroud)
我试过这个,但它不起作用:
responseObj = readJsonFromUrl('http://query.yahooapis.com/v1/publ...');
var count = responseObj.query.count;
console.log(count) // should be 1
Run Code Online (Sandbox Code Playgroud)
如何从此URL的JSON响应中获取JavaScript对象?
我正在尝试运行onClick任何按钮的功能onclick.我在Firebug中收到错误
document.getElementByClass不是函数
这是我的代码:
var stopMusicExt = document.getElementByClass("stopButton");
stopButton.onclick = function() {
var ta = document.getElementByClass("stopButton");
document['player'].stopMusicExt(ta.value);
ta.value = "";
};
Run Code Online (Sandbox Code Playgroud) 我有一些像这样的代码,
<form id="abc">
<input type="text" id="txt" />
</form>
Run Code Online (Sandbox Code Playgroud)
现在我想像这样重定向,
var temp = $("#txt").val();
url = "http://example.com/" + temp;
window.location.replace(url);
// or window.location(url);
Run Code Online (Sandbox Code Playgroud)
无论如何在jQuery中解决这个问题?它仍然让我拥有url = http://example.com.
我在我的HTML中有1个按钮和一些文本,如下所示:
function get_content(){
// I don't know how to do in here!!!
}
<input type="button" onclick="get_content()" value="Get Content"/>
<p id='txt'>
<span class="A">I am</span>
<span class="B">working in </span>
<span class="C">ABC company.</span>
</p>
Run Code Online (Sandbox Code Playgroud)
当用户单击该按钮时,该内容<p id='txt'>将成为以下内容
预期结果:
<p id='txt'>
// All the HTML element within the <p> will be disappear
I am working in ABC company.
</p>
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我如何编写javascript函数?谢谢.
我在表单中有一个复选框,我希望它按照以下方案工作:
如果有人检查它,textfield(totalCost)的值应该设置为10.然后,如果我返回并取消选中它,函数会根据表单中的其他参数calculate()设置值totalCost.
所以基本上,我需要的部分在哪里,当我选中复选框时我会做一件事,当我取消选中它时,我会做另一件事.
如何使用jquery将某个值正确加载到文本框中?尝试下面的那个[object Object],但我得到了输出.请赐教我,我是jquery的新手.
proc = function(x, y) {
var str1 = $('#pid').value;
var str2 = $('#qtytobuy').value;
var str3 = $('#subtotal').load('compz.php?prodid=' + x + '&qbuys=' + y);
$('#subtotal').val(str3);
}Run Code Online (Sandbox Code Playgroud)
这是html表单:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form name="yoh" method="get">
Product id: <input type="text" name="pid" value=""><br/>
Quantity to buy:<input type="text" name="qtytobuy" value="" onkeyup="proc(document.yoh.pid.value, this.value);"></br>
Subtotal:<input type="text" name="subtotal" id="subtotal" value=""></br>
<div id="compz"></div>
</form>Run Code Online (Sandbox Code Playgroud) tl; dr:是否可以制作可重复使用的模板文字?
我一直在尝试使用模板文字,但我想我只是没有得到它,现在我感到沮丧.我的意思是,我认为我得到它,但"它"不应该是它如何工作,或它应该如何得到.应该有所不同.
我看到的所有示例(甚至是标记模板)都要求"替换"在声明时而不是运行时完成,这对我来说对于模板来说似乎完全没用.也许我很疯狂,但对我来说,一个"模板"是一个包含令牌的文档,当你使用它时,它们会被替换,而不是在你创建它时,否则它只是一个文档(即一个字符串).模板与令牌一起存储为令牌,当您评估它们时,将评估这些令牌.
每个人都引用了一个可怕的例子,类似于:
var a = 'asd';
return `Worthless ${a}!`
Run Code Online (Sandbox Code Playgroud)
这很好,但如果我已经知道a,我会return 'Worthless asd'或者return 'Worthless '+a.重点是什么?认真.好吧,重点是懒惰; 更少的优点,更多的可读性.大.但那不是模板!不是恕我直言.MHO就是最重要的!问题,恕我直言,模板在声明时被评估,所以,如果你这样做,恕我直言:
var tpl = `My ${expletive} template`;
function go() { return tpl; }
go(); // SPACE-TIME ENDS!
Run Code Online (Sandbox Code Playgroud)
由于expletive未声明,因此输出类似的内容My undefined template.超.实际上,至少在Chrome中,我甚至无法声明模板; 它会抛出错误,因为expletive没有定义.我需要的是能够在声明模板后进行替换:
var tpl = `My ${expletive} template`;
function go() { return tpl; }
var expletive = 'great';
go(); // My great template
Run Code Online (Sandbox Code Playgroud)
但是,我不知道这是如何可能的,因为这些不是真正的模板.即使你说我应该使用标签,nope,它们也不起作用:
> explete = function(a,b) { console.log(a); …Run Code Online (Sandbox Code Playgroud) 这是检查字段值是否为空的好方法吗?
if($('#person_data[document_type]').value() != 'NULL'){}
Run Code Online (Sandbox Code Playgroud)
或者,还有更好的方法?
javascript ×10
jquery ×3
checkbox ×1
ecmascript-6 ×1
forms ×1
html ×1
json ×1
messagebox ×1
php ×1
redirect ×1
url ×1