附加代码以形成成功消息的脚本将图像放置在消息 DIV 之外
我正在使用 ajax 脚本在成功消息中生成文本。该过程的最后一部分是添加一个关闭图像 DIV,单击它时应关闭(向上滑动)包含表单和成功消息的“面板”DIV,就像单击关闭图像而不发送任何消息时发生的情况一样。您可以通过点击此页面上导航栏中的“联系人”来查看该表单。
这是生成成功消息的脚本:
$.ajax({
type: "POST",
url: "contact-engine.php",
data: dataString,
success: function() {
$('#contact-form').html("<div id='message-form'></div>");
$('#message-form').html("<h3>Your message has been submitted successfully!</h3>")
.hide()
.fadeIn(2000, function() {
$('<p style="text-align:center">Thanks for getting in touch. I will get back to you as soon as possible.</p>').appendTo('#message-form').hide().fadeIn(2000, function() {
$('<p style="text-align:center"><a class="close" href="#"><img src="/images/close.png"></a></p>').appendTo('#message-form').hide().fadeIn(2000);
});
});
}
});
这是处理关闭按钮 DIV 上的单击功能的脚本:
$(".close").click(function ()
{
$("#panel").slideUp("slow");
$("li#contact").removeClass("current");
// $("#contact").removeClass("current");
$panel.visible = false;
return false;
});
有人可以让我知道为什么关闭按钮位于 DIV 之外以及为什么它不会从以下位置关闭表单吗?成功消息?
谢谢,
尼克
I am using an ajax script to generate text in a success message. The final part of the process is to add a close image DIV which when clicked should close (slide up) the 'panel' DIV which contains the form and the success message, as happens when the close image is clicked on without sending any message. You can see the form by clicking on 'contact' in the nav bar on this page.
Here is the script that generates the success message:
$.ajax({
type: "POST",
url: "contact-engine.php",
data: dataString,
success: function() {
$('#contact-form').html("<div id='message-form'></div>");
$('#message-form').html("<h3>Your message has been submitted successfully!</h3>")
.hide()
.fadeIn(2000, function() {
$('<p style="text-align:center">Thanks for getting in touch. I will get back to you as soon as possible.</p>').appendTo('#message-form').hide().fadeIn(2000, function() {
$('<p style="text-align:center"><a class="close" href="#"><img src="/images/close.png"></a></p>').appendTo('#message-form').hide().fadeIn(2000);
});
});
}
});
And here is the script that handles the click function on the close button DIV:
$(".close").click(function ()
{
$("#panel").slideUp("slow");
$("li#contact").removeClass("current");
// $("#contact").removeClass("current");
$panel.visible = false;
return false;
});
Could someone please let me know why the close button is outside the DIV and why it won't close the form from the success message?
Thanks,
Nick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我刚刚在您链接到的网站上测试了它。关闭按钮实际上并不在 div 之外,它只是出现在您希望其显示的区域下方。这是因为包装
的高度为 0。并且它有一个高度为 0,因为您正在浮动其内容(
标记)。只需给
高度为
35px
就可以了:我猜点击处理程序不会触发,因为
.当您尝试绑定
元素不存在。只需将所有click
事件时,相关的 close$('.close').click(...)
代码放在创建.close
元素的行后面即可:I just tested it on the site you link to. The close button is not actually outside of the div, it just appears below the area you want it to show up in. That's because the wrapping
<p>
has a height of 0. And it has a height of 0 because you are floating its contents (the<a>
tag). Just give that<p>
a height of35px
and you'll be fine:I'm guessing that the click handler doesn't fire because the
.close
element in question doesn't exist when you try binding theclick
event. Just put all that$('.close').click(...)
code after the line in which you create the.close
element:这是一个快速的 jsFiddle
http://jsfiddle.net/MRaeq/3/
您添加的所有内容似乎正确的。但是单击事件看起来可能需要在创建关闭按钮后添加实时处理程序,除非您已经处理过它
Here is a quick jsFiddle
http://jsfiddle.net/MRaeq/3/
Everything with you add seems right. but the click event looks like it may need a live handler to be added once the close button is created unless you were already handling it
我在这里放了一个示例(有更改): http://jsfiddle.net/MarkSchultheiss/bUWh4/
不太漂亮,但您应该能够提取您需要的内容,并且单击按钮可以通过更改为 .on() 来工作
I put an example (with changes) in here: http://jsfiddle.net/MarkSchultheiss/bUWh4/
Not really that pretty, but you should be able to extract what you need, and the click on the button works by changing to .on()
您的问题是
$(".close").click(function() { ... })
方法仅绑定 DOM 中当前存在的选定元素。您将不得不使用其他一些方法,例如:至于 div 外部的关闭按钮,我不太确定您指的是哪个 div 以及您所看到的确切内容。我建议按如下方式清理您的 AJAX:
当然,除非您确实打算让事情逐渐淡出。希望这有帮助!
Your problem is that the
$(".close").click(function() { ... })
method only binds selected elements that currently exist in the DOM. You will have to use some other means such as:As for the close button being outside the div, I'm not quite sure which div you mean and exactly what you're seeing. I would suggest cleaning up your AJAX as follows:
Unless of course you do intend to have things fade in progressively. Hope this helps!