jquery - 将 html 附加到 JQUERY AJAX 调用的末尾
问题描述
我正在尝试做某事,经过互联网研究,我不清楚我是否可以。我想在脚本末尾添加模态的 HTML,所有这些都在 AJAX Success 调用中。这可能吗?这是我尝试过的代码:
function options() {
jQuery.ajax({
type: 'post',
url: my_ajax.ajax_url,
data: {
action: 'test2_update_function'
},
success: function(data) {
jQuery("#myModal").show(function() {
// Get the modal
var modal = document.getElementById("myModal");
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
}
/*>>> closes the .show*/
).append('<div id="myModal" class="modal"><div class="modal-content"><span class="close">×</span><p id="my-data"></p></div></div>');
jQuery("#my-data").html(data); //Add this line
;
}
});
}
解决方案
我认为你的意思是这样的
$(function() {
$("body").append(`<div id="myModal" class="modal">
<div class="modal-content"><span class="close">×</span>
<p id="my-data"></p>
</div>
</div>`); // if you must
$("#btn").on("click", function() {
$("#modal").show()
})
$("#modal").on("click", ".close", function() {
$("#modal").hide();
})
$(document).on("click", function(e) {
if ($(e.target).closest("#modal").length === 0 ) {
$("#modal").hide()
}
})
})
function options() {
$.ajax({
type: 'post',
url: my_ajax.ajax_url,
data: {
action: 'test2_update_function'
},
success: function(data) {
$("#my-data").html(data); //Add this line
$("#myModal").show()
}
});
}
<div id="myModal" class="modal">
<div class="modal-content"><span class="close">×</span>
<p id="my-data"></p>
</div>
</div>
推荐阅读
- flask - config.py 在 Flask 上带有 'if' 语句
- c# - 如何在 WPF 中的 Material Design Pack 图标上进行点击事件?
- signature - 什么是呼叫签名?
- c - C中损坏的堆,我不知道为什么
- c++ - C++ 中的动态向量创建
- json - Python中的CSV到JSON块
- dart - 如何知道哪个屏幕在顶部(活动/已安装)
- javascript - 如何在 JavaScript 中设置画布的属性
- multithreading - Web 服务器中长时间运行的任务
- javascript - 在 html 中导入脚本:sjcl.hash.sha512 未定义