remove()在普通WordPress REST API中不起作用



remove((方法在我的Vanilla Javascript代码中不起作用,即使Element被正确选择(在控制台中记录"DeletedBlock"会正常返回(。

我尝试选择父对象,但没有成功。我甚至试着简单地添加一个";隐藏的";类,但也不起作用。

帖子确实会被删除,但元素不会被删除

这是我的密码。。。


HTML

<div class="student-names-in-block">
</div>
<div>
<div class="block-text-contents">
<span class="highlight-text"></span>
<span class="block-text"></span>
</div>
<a class="day-time" target="_blank" href="http://te-io.local/class-description/tuesday-2000-2-2/">tuesday 20:20-21:20</a>
<div class="block-buttons">
<a target="_blank" class="room-button" href="https://whereby.com/topenglish">Top</a><a target="_blank" class="report-button" href="http://te-io.local/class-description/tuesday-2000-2-2/">Report</a>
</div>
</div>
<div class="block-buttons admin-buttons">
<a class="delete-button">Delete</a>
</div>
。。。

JavaScript

...
//Delete the clicked class
deleteClassDesc(classID){
const http = new XMLHttpRequest()        
const params = '?class-description=' + classID
http.open("DELETE", scheduleLocalize.teSiteUrl + "/wp-json/top/v1/schedule" + params , true)
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
http.setRequestHeader("X-WP-Nonce", scheduleLocalize.nonce)
http.onreadystatechange = function(){
if (http.readyState == 4){
if(http.status == 200){
console.log('Success! Class ID: '+ classID)
const theDeletedBlock = document.querySelector(`[data-class-id='${classID}']`)

//this line does not work
theDeletedBlock.remove()
}else if (http.status == 400) {
console.log('Admin: There was an error 400. Please try again, or contact us.');
}
else {
//something else other than 200 was returned
console.log(`Admin: Error: Please try again, or contact us.`);
}
}
}

http.send();
//alert('Delete class ID: ')
}

解决了它!

所以,html是从另一个JS文件生成的,所以我似乎是在一个父DIV之外生成html的这个特定部分,或者更具体地说,在生成每一代子块的正确JS循环之外。

我不知道确切的原因,但在生成html的JS中组织起来解决了问题。

感谢回答的人!

最新更新