jquery reset after()

2019-08-09 10:07发布

is there a way to reset/update an after() element? Not add another after() text. Thank you

4条回答
欢心
2楼-- · 2019-08-09 10:41

Does after have a callback parameter? If so you can use that to call a function with what you'd want to do.

查看更多
走好不送
3楼-- · 2019-08-09 10:41

Your question is not clear. I'll asume you want to modify an element added with .after()

Instead of doing this:

$("#elem1").after('<div id="after />");

You could do this (use insertAfter)

$('<div id="after" />').insertAfter("#elem1").attr("width", 200).html("hi") ...;

Hope this helps. Cheers.

查看更多
一夜七次
4楼-- · 2019-08-09 10:42

When you add the element, give it a name

var newElement = $('<span>Some new stuff</span>');
$('.whatever').after(newElement);

Then, when you want to change it, simply remove the previous one first

newElement.remove();
newElement = $('<div>And now for something completely different</div>');
$('.whatever').after(newElement);

You can write a function that uses .data() to remember the new element as such: (I would change the names a bit though)

$.fn.addUniqueSomething = function (content) {
    var existing = this.data('something-that-was-already-added');
    if (existing) {
        existing.remove();
    }

    var something = $(content);
    this.after(something);
    this.data('something-that-was-already-added', something);
};

Then you can use

$('.whatever').addUniqueSomething('<span>Some new stuff</span>');
// and later...
$('.whatever').addUniqueSomething('<div>And now for something completely different</div>');

And the second one will replace the first

查看更多
倾城 Initia
5楼-- · 2019-08-09 11:05

Maybe this will helpful.

(Controller function for Emptiness of Form to be sent Server Input parameter ID of Form Parent DIV, output is 1-true, 0 false)

 function emptynessCntrl(elementosForControl){
      var controlResult=1;
        $(elementosForControl).find('input').each(function() {
            if($(this).val().trim()===""){
              controlResult=0;
              console.log($(this).attr('id')+' Element is empty');
              $(this).nextAll().remove();
              $(this).after('<div class="err-label">'+$(this).attr('placeholder')+' is empty</div>');
              return controlResult;
              } 
              else{
                $(this).nextAll().remove();
              }        
           });
        return controlResult;
       }
查看更多
登录 后发表回答