Removing a div added using the add method

this is my code ... append works, but uninstall does not work. how to remove added div?

<html>
  <head>
    <script type="text/javascript">
      function view(){
        $('body').append('<div class="added"><p>something</p></div>');
      };
      function close(){
        $('.added').remove();
      } ;
    </script>
  </head>
  <body>
    <a onclick="view();">something</a>
    <a onclick="close();">close</a>
  </body>
</html>
+5
source share
2 answers

Indicate window.closein your html handler:

   <a onclick="window.close();">close<a>

http://jsfiddle.net/ZTwd7/1/

Otherwise, it close()refers to its own method close, which does nothing.

This is how you emulate it using the javascript handler (as opposed to the built-in html attribute):

elem.onclick = function(event) {
    with(Window.prototype) {
        with(document) {
            with(this) {
                close();
            }
        }
    }
};

This is not an exact reproduction (and it does not work in IE, etc.), but it puts .closein Window.prototypefront of the global window.closein scope, so it is shadow, you can still reference it using window.close().


jQuery:

<a id="view">something<a>
<a id="close">close<a>

JS:

$(function() {
    $("#view").click(function() {
        $('body').append('<div class="added"><p>something</p></div>');

    });
    $("#close").click(function() {
        $('.added').remove();

    });
});​

http://jsfiddle.net/ZTwd7/5/


ajax, html, :

$.get("myhtml.html", function(html) {
    $("body").append(html);
}, "html");
+5

, , , close(),

<html>
    <head>
<script src="http://code.jquery.com/jquery-latest.js"></script>

   <script type="text/javascript">
            function view(){
                     $('body').append('<div class="added"><p>something</p></div>');
            };
            function close_it(){
                                       $('.added').remove();

            } ;

    </script>
</head>
<body>
   <a onclick="view();">something<a>
   <a onclick="close_it();">close<a>
</body></html>
+1