【发布时间】:2017-12-22 09:00:18
【问题描述】:
我根据使用display: none 的ajax 方法的响应隐藏了<div>。如果我想在再次调用 AJAX 方法时显示相同的 <div> 怎么办?
我想知道display: none 是否真的从DOM 中删除了元素,因为使用display: block 不会使<div> 再次可见。如何使用 display 属性显示和隐藏相同的 div。这是我用于 AJAX 调用的代码:
$.ajax({
type: "POST",
url: "Request.aspx/FireEvents",
data: JSON.stringify({ controlValues: pageControlNamesAndValues }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var controlVals = response.d;
$.each(controlVals, function (index, currentControl) {
var targetDropDown = document.getElementById(ctrlName);
console.log('Targetdropdown is ' + targetDropDown);
if (targetDropDown != null) {
if (targetDropDown instanceof HTMLDivElement) {
if (currentControl.ControlName == "LogicChecks") {
if (currentControl.ControlValue = "hidden") {
targetDropDown.style.display = 'none';
} else {
targetDropDown.style.display = 'block';
}
}
}
}
});
}
});
最后一行 (...style.display ='block';) 隐藏后不会显示 <div>。
【问题讨论】:
-
你能确认
success函数被调用了吗?您能确认找到该元素吗?您可以使用 javascript 调试控制台(在任何主流浏览器中)来测试代码行,看看它们是否对 DOM 有任何影响。display: none不会从 DOM 中移除元素。 -
现在就使用 jQuery。
$("#"+ ctrlName).show()或$("#"+ ctrlName).hide()假设你在某处有 ctrlName(我看不到它) -
是的,成功的方法总是被执行。是的,该元素将在初始页面加载时找到。但是基于这个 ajax 响应我们需要隐藏和显示一些 div 的
-
这是零意义:
var targetDropDown = document.getElementById(ctrlName); if (targetDropDown == null) { targetDropDown = document.getElementById(ctrlName); } -
如果你在
targetDropDown变量中有一个元素,那么targetDropDown.style.display = 'block';确实应该确保它被显示,因为它本质上将其内联样式(覆盖类等)设置为display: block;......所以问题很可能是您的脚本是否曾经到达发生这种情况的行 - 您是否尝试在显示设置为阻止之前放置控制台日志记录调用?
标签: javascript jquery css asp.net ajax