我有一個錯誤需要解決document.querySelector('#myAjax'):TypeError: document.querySelector(...) is null
在我的代碼下面。有什么問題 ?謝謝
<div id="#myAjax"></div>
<script>
window.addEventListener("DOMContentLoaded", (event) => {
console.log("DOM uploaded and analysed");
document.querySelector('#myAjax')
.addEventListener('click', function(e) {
let selectedOptionVal = document.querySelector('#move_to_category_id').value,
options_html = "";
fetch("{$categories_ajax}?" selectedOptionVal)
.then(function(response) {
return response.json();
})
.then(function(jsonResponse) {
// Ajax success
console.log("data is :", jsonResponse);
for (const index in jsonResponse) {
let category_id = jsonResponse[index].id;
let category_name = jsonResponse[index].text;
let selectedString = category_id == selectedOptionVal ? ' selected="selected"' : '';
options_html = '<option value="' category_id '"' selectedString '>' category_name '</option>';
}
$('#move_to_category_id').html(options_html);
})
.catch(function(err) {
// error ajax
alert("error :" err);
});
});
});
</script>
uj5u.com熱心網友回復:
CSS id 選擇器使用 HTML 元素的 ID 屬性來選擇頁面上的一個唯一元素。要在 CSS 中使用 ID 選擇器,您需要撰寫一個主題標簽,#后跟元素的 ID,但沒有必要并且實際上非常令人困惑和錯誤#的是在屬性值中添加。
最重要的是,您需要#從 id 屬性中洗掉。將其更改為:
<div id="#myAjax"> // wrong
到
<div id="myAjax"> // correct
uj5u.com熱心網友回復:
#從組件 div id 中洗掉:<div id="myAjax"></div>
uj5u.com熱心網友回復:
<div id="#myAjax">
在這一行你不應該使用'#'
使用
<div id="myAjax">
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/400024.html
標籤:javascript
