在元素后插入 - 逻辑帮助
我有以下内容:
$.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
} 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');
因此,如果有东西到达,我将用图像替换内容。 这有效。
问题是,在第二种情况(e.msg ==3)
我不应该不删除或用图像替换所有内容,但是 我需要在特定位置附加一个元素 p 说:“某事”。
我试图显示:无;该元素然后添加:
else if(e.msg == 3){
myParagrafId.show();
}
什么也没有出现。
我尝试:
else if(e.msg == 3){
$('#foo').append('<p>Something</p>');
}
仍然没有...
该函数似乎正在返回 html,忽略第二个条件中的指令。
那么,也许我需要获取现有的 html 部分,附加我需要的内容,然后允许该函数输出它?
附加说明: 如果我发出警报();在该条件下(e.msg == 3)它可以工作。
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
} 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您总是将
html
放入$("#msg")
中,因此您应该检查它不是空字符串:
因此,如果您尝试在其中执行某些操作
$("#msg")
(就像在其中显示一个隐藏的 div)在情况 3 中它什么也不做,因为它被 '' 替换,因为你正在调用$("#msg") .html('');
这有帮助吗?
否则你就做错了,如果你显示你的标记,我们可以提供帮助
First af all you are always putting
html
into$("#msg")
hereso you should check it's not an empty string:
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