开发者

Insert after an element - logic help

开发者 https://www.devze.com 2023-03-13 13:50 出处:网络
I have the following: $.post(baseUrl+\"/index/voto\",{id:eid},function(e){ var divMsg = $(\"#msg\"); var html = \"\";

I have the following:

$.post(baseUrl+"/index/voto",{id:eid},function(e){
 var divMsg = $("#msg");
 var html = "";
   if(e.msg == 1){
     html = '<img src="'+baseUrl+'/lib/img/showImageA.png" alt="pdf" />';
   }else if(e.msg == 3){
     //HERE I MUST DISPLAY A TEXT AFTER A GIVEN ELEMENT
 开发者_如何学Python  } else if(e.msg == 2){
     html = '<img src="'+baseUrl+'/lib/img/showImageB.png" alt="pdf" />';
   } else{
     html = '<img src="'+baseUrl+'/lib/img/showImageC.png" alt="pdf" />';
   }
   divMsg.show();
   divMsg.html(html); 

}, 'json');

So, if something arrives, I will replace the content with an image. This works.

The issue is that, on the second case (e.msg ==3) I should not remove or replace the all thing with an image, but I need to append an element p on a specific place saying: "something".

I tried to display:none; that element and then add:

else if(e.msg == 3){
 myParagrafId.show();
}

Nothing appears.

I tried to:

else if(e.msg == 3){
 $('#foo').append('<p>Something</p>');
}

Still nothing...

The function seems to be returning the html, ignoring my instructions inside the second conditional.

So, perhaps I need to grab the existing html part, append what I need, and then allow the function to output it ?

Additional note: If I place an alert(); inside that condition (e.msg == 3) it works.


First af all you are always putting html into $("#msg") here

//in case 3 html = ''
divMsg.html(html);

so you should check it's not an empty string:

if (html != ''){
    divMsg.html(html);
}

So if you were trying to do somenthing inside $("#msg") (like showing an hidden div inside it) in case 3 it does nothing because it gets replaced by '' because you are calling $("#msg").html('');

does this help?

Otherwise you are doing something wrong in that if, if you show your markup we can help

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号