简体   繁体   English

Javascript删除div元素

[英]Javascript delete div Element

I am implementing a Stopwatch in JavaScript. 我正在JavaScript中实现一个秒表。

I want to delete additional div elements when pushing a clear button. 我想在按下clear按钮时删除其他div元素。 I added function cc(); 我添加了函数cc(); , but function cc() doesn't work. ,但function cc()不起作用。 What's wrong with my code? 我的代码出了什么问题?

Javascript: 使用Javascript:

a = 0;
myButton = 0;

function myWatch(flug) {
  if(myButton == 0) {
    Start = new Date();
    myButton = 1;
    document.myForm.myFormButton.value = "Stop!";
    myInterval = setInterval("myWatch(1)", 1);
  } else {
    if(flug == 0) {
      myButton = 0;
      document.myForm.myFormButton.value = "Start";
      clearInterval(myInterval);
    }
    Stop = new Date();
    T = Stop.getTime() - Start.getTime();
    H = Math.floor(T / (60 * 60 * 1000));
    T = T - (H * 60 * 60 * 1000);
    M = Math.floor(T / (60 * 1000));
    T = T - (M * 60 * 1000);
    S = Math.floor(T / 1000);
    Ms = T % 1000;
    document.myForm.myClick.value = H + ":" + M + ":" + S + ":" + Ms;
  }
}
b = 0;

function stop() {
  b++;
  stopa();
}

function stopa() {
  var element = document.createElement('div');
  element.id = pos;
  var objBody = document.getElementsByTagName("body")
    .item(0);
  objBody.appendChild(element);
  rap = "";
  rap = "rap" + b + "=" + H + ":" + M + ":" + S + ":" + Ms;
  element.innerHTML = rap;
}

function cc() {
  document.body.removeChild(element);
}

HTML: HTML:

<form name="myForm" action="#">
  <input type="text" size="20" name="myClick">
  <input type="button" value="Start" name="myFormButton" onclick="myWatch(0)" />
  <input type="button" value="Rap" name="Rap" onclick="stop()">
  <input type="button" value="clear" name="clear" onclick="cc()">
</form>
<h3>
  <div id="pos">
  </div>
</h3>

Try to pass the context explicitly as parameter on the cc() function. 尝试将上下文作为参数显式传递给cc()函数。

js JS

function cc(el) {
    el.parentNode.removeChild(el);
}

html HTML

<input type="button" value="clear" name="clear" onclick="cc(this)">

Open up your Console. 打开你的控制台。 It's found in most major browsers and will help you loads with JS development. 它存在于大多数主流浏览器中,可以帮助您加载JS开发。

For example, if you look at your code exactly as is , you get an error: 例如, 如果您完全按原样查看代码 ,则会收到错误消息:

Uncaught ReferenceError: element is not defined

As several have said, it's a variable scope issue . 正如几位人士所说,这是一个可变范围问题 There's a few easy fixes, the easiest being simply making element have a larger scope - that is, define it outside of your functions first. 有一些简单的修复,最简单的是简单地使element具有更大的范围 - 也就是说,首先在函数之外定义它。

var element = null;

and then in stopa , don't use var because it'll make a new, local variable instead of using the global one. 然后在stopa ,不要使用var因为它将创建一个新的局部变量,而不是使用全局变量。

element = document.createElement('div');

The next problem you'll notice is that element in cc still doesn't refer to the correct element - this is because in the following line, pos is not defined. 您将注意到的下一个问题是cc中的element仍未引用正确的元素 - 这是因为在以下行中,未定义pos

element.id = pos;

Again, take a look at the Console (now more specifically at the HTML section than the JS, but it's in the same developer options). 再次,看看控制台(现在更具体地说,在HTML部分而不是JS,但它在相同的开发人员选项中)。 You'll notice the created element's ID is [object HTMLDivElement] - not "pos". 你会注意到创建的元素的ID是[object HTMLDivElement] - 而不是“pos”。 I'd recommend not even worrying about giving them id s, you have a div with id pos already in your HTML, simply append your results to that instead of the body . 我建议你甚至不要担心给他们id ,你的HTML中已经有了一个id为pos的div,只需将你的结果追加到那个而不是body

objBody = document.getElementById("pos");

Then when you clear, clear all children from pos (you'll also want to clear the text box I'm sure, so here ya go) 然后当你清除时,清除所有来自pos孩子(你也要清除文本框我敢肯定,所以你走了)

while (objBody.hasChildNodes()) {
  objBody.removeChild(objBody.lastChild);
}
document.myForm.myClick.value = "";

Notice now we no longer need element to be global, because it's only referenced in one function - it can be local to that function. 现在请注意,我们不再需要将element设置为全局element ,因为它只在一个函数中引用 - 它可以是该函数的本地element However, objBody is now referenced in stopa and cc , so we'll make it global as we did with element and simply ensure all future references to it don't include var . 但是, objBody现在在stopacc被引用,所以我们将它与element一样全局化,并且只是确保将来对它的所有引用都不包含var

var element = null;

Add some error checking (clearing before you click Start causes errors, for example) and you're good to go! 添加一些错误检查(例如,在单击“开始”之前清除会导致错误),您就可以开始了!

http://jsfiddle.net/daCrosby/MMywX/1/ http://jsfiddle.net/daCrosby/MMywX/1/

The function cc has no idea what 'element' is. 函数cc不知道'元素'是什么。 The easiest way to fix this is to use jquery. 解决这个问题的最简单方法是使用jquery。 When you delete the div, do something like 删除div时,请执行类似的操作

$('#pos').remove()

This finds the div with id 'pos' and removes it. 这将找到id为'pos'的div并将其删除。 You may need to pass the id of the div to cc if it's not just 'pos'. 如果它不仅仅是'pos',你可能需要将div的id传递给cc。

Modern browsers supports remove() method on the element. 现代浏览器支持元素上的remove()方法。 You don't need to use external libraries as jQuery or long code. 您不需要将外部库用作jQuery或长代码。 It's simple as : 这很简单:

var x = document.getElementById("myElement");
x.remove();

Working exemple here -> https://jsfiddle.net/tmj3p7t5/ 这里的工作例子 - > https://jsfiddle.net/tmj3p7t5/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM