如何检查Div是否为空然后执行操作 [英] How to Check if a Div is Empty and Then Execute Action
问题描述
我正在用HTML5,CSS和< Javascript
这就是我当前的 问题所在。
我有一个< div>
,其中包含脚本,其中包含有关何时询问问题的答案以及问题是否与写入该答复的答案相匹配的答案。
我需要一个脚本来检查这个div是否为空,在 Javascript 中,而不是 jQuery
我需要检查div是否为空,因为这意味着没有响应。
然后为了重定向到错误页面。a问题&对此的回应如下所示:
< script type =text / javascript>
{
var q = getValue(q);
var name = getValue(name);
}
if(q =='Whats + up'){
document.write(Not much+ name +。);
}
else {
}
< / script>
这是我试过的最后一段代码:
< script>
var theDiv = document.getElementById(responce);
var q = getValue(q);
if(theDiv.innerHTML ==''){
window.location ='error.html?name = Brandon& q ='+ q;
}
< / script>
您可以检查,如果没有其他元素脚本在
div
中:
<脚本>
var theDiv = document.getElementById('responce'),
q = getValue('q'),
scripts = theDiv.getElementsByTagName('SCRIPT')。length;
if(theDiv.children.length - scripts === 0){
window.location ='error.html?name = Brandon& q ='+ q;
}
< / script>
但是,我建议你摆脱所有内联脚本,并将它们放在 head
,或者在结束 body
标记之前。
编辑
快速修复的方法是将脚本
移到下面 #responce
。
I'm Building a Personal Assistant with HTML5, CSS, & Javascript
This is what my current Problem is.
I Have a <div>
that contains scripts with the responses for when a Question is asked and if the question matches one it writes that response.
I Need a Script that will check if this div is empty, in Javascript, not jQuery
I need to check if the div is empty because that means there is no response.
Then for it to redirect to an the error page.
a Question & the response to it would look like this:
<script type="text/javascript">
{
var q = getValue("q");
var name = getValue("name");
}
if (q == 'Whats+up') {
document.write("Not much " + name + ".");
}
else {
}
</script>
This is the last code I tried:
<script>
var theDiv = document.getElementById("responce");
var q = getValue("q");
if(theDiv.innerHTML == ''){
window.location = 'error.html?name=Brandon&q='+q;
}
</script>
You could check, if there are no other elements than script
s in the div
:
<script>
var theDiv = document.getElementById('responce'),
q = getValue('q'),
scripts = theDiv.getElementsByTagName('SCRIPT').length;
if (theDiv.children.length - scripts === 0) {
window.location = 'error.html?name=Brandon&q=' + q;
}
</script>
However, I'd suggested you to get rid of all inline scripts, and put them in the head
of the document or just before the closing body
tag instead.
EDIT
A quick-fix would be to move the script
below the #responce
.
这篇关于如何检查Div是否为空然后执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!