【发布时间】:2019-11-05 16:49:42
【问题描述】:
我发出一个 Ajax 请求,之后我更改了一个属性值。在 DOM 中,一切都按预期变化。但是当我再次单击该按钮时,旧的属性值会显示在控制台中。似乎他没有看到标记的变化并加载了旧数据。
$('.more_credits_first').click(function () {
var button = $(this)
var show = $(button).data('show');
console.log(show)
if (show == 'show') {
$.get(window.location.pathname,{}).done(function(data){
$('.tbody.main_credits').append(data.template);
$(button).attr('data-show', 'hide');
$(button).text('Hide');
})
} else {
$(this).attr('data-show', 'show');
$(this).text('More');
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class='more_credits_first' type="button" data-show='show'>Button</button>
【问题讨论】:
-
Access to XMLHttpRequest at 'https://stacksnippets.net/js' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.—done函数永远不会被调用,因此data-show在您的示例中永远不会被修改。
标签: javascript jquery html ajax dom