gpt4 book ai didi

javascript - 浏览器不会反射(reflect) JS 文件中的更改

转载 作者:行者123 更新时间:2023-11-28 00:21:12 24 4
gpt4 key购买 nike

我最近正在学习如何托管本地网络服务器,最近我一直在处理一个非常令人沮丧的问题。我的浏览器看到我在 HTML 和 CSS 文件中所做的更改,但它根本不会响应我的 JS 文件。它们都在 index.js、index.html 和 main.css 下的同一个文件夹中。当我什至无法判断自己所做的是否有效时,这对我来说非常令人沮丧和难以练习。有没有人看到我的代码有任何问题可以解释这一点?我禁用了缓存,所以我知道不是这样。

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
<title>Shopping List</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/4.2.0/normalize.min.css">
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<link rel="stylesheet" href="main.css">
</head>
<body>
<div class="container">
<h1>Shopping List</h1>

<form id="js-shopping-list-form">
<label for="shopping-list-entry">Add an item</label>
<input type="text" name="shopping-list-entry" id="shopping-list-entry" placeholder="e.g., broccoli">
<button type="submit">Add item</button>
</form>
</div>
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<script src="index.js"></script>
</body>
</html>

JS:

'use strict';
/* global $ */

function addItem(){
$('input').prop('required');
$('#js-shopping-list-form').submit(event => {
event.preventDefault();
const userTextElement = $(event.currentTarget).find('#shopping-list-entry');

helpAdd(userTextElement.val());
});
}

function checkItem(){
$('.shopping-list').on('click', '.shopping-item-toggle', event => {
event.preventDefault();
});
}

function deleteItem(){
$('.shopping-list').on('click', '.shopping-item-delete', event => {
event.preventDefault();
});
}

function helpCheck(item) {
if($(item.closest('li')).find('>:first-child').attr('class') === 'shopping-
item shopping-item__checked')
{
$(item.closest('li')).find('>:first-child').attr('class','shopping-item');
}
else{
$(item.closest('li')).find('>:first-child').attr('class','shopping-item
shopping-item__checked');
}
}

function helpAdd(itemName){
$( '.shopping-list').append(` <li>
<span class="shopping-item">${itemName}</span>
<div class="shopping-item-controls">
<button class="shopping-item-toggle">
<span class="button-label">check</span>
</button>
<button class="shopping-item-delete">
<span class="button-label">delete</span>
</button>
</div>
</li>`);
}

$(addItem);
$(checkItem);
$(deleteItem);

最佳答案

看起来您缺少我刚刚添加为 <ul class="shopping-list"></ul> 的“购物 list ”类元素

请试试这段代码

'use strict';
/* global $ */

function addItem() {
$('input').prop('required');
$('#js-shopping-list-form').submit(event => {
event.preventDefault();
const userTextElement = $(event.currentTarget).find('#shopping-list-entry');

helpAdd(userTextElement.val());
});
}

function checkItem() {
$('.shopping-list').on('click', '.shopping-item-toggle', event => {
event.preventDefault();
});
}

function deleteItem() {
$('.shopping-list').on('click', '.shopping-item-delete', event => {
event.preventDefault();
});
}

function helpCheck(item) {
if ($(item.closest('li')).find('>:first-child').attr('class') === 'shopping-item shopping - item__checked')
{
$(item.closest('li')).find('>:first-child').attr('class', 'shopping-item');
}
else {
$(item.closest('li')).find('>:first-child').attr('class', 'shopping-item shopping - item__checked');
}
}

function helpAdd(itemName) {
$('.shopping-list').append(` <li>
<span class="shopping-item">${itemName}</span>
<div class="shopping-item-controls">
<button class="shopping-item-toggle">
<span class="button-label">check</span>
</button>
<button class="shopping-item-delete">
<span class="button-label">delete</span>
</button>
</div>
</li>`);
}

$(addItem);
$(checkItem);
$(deleteItem);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<h1>Shopping List</h1>

<form id="js-shopping-list-form">
<label for="shopping-list-entry">Add an item</label>
<input type="text" name="shopping-list-entry" id="shopping-list-entry" placeholder="e.g., broccoli">
<button type="submit">Add item</button>
</form>

<ul class="shopping-list"></ul>
</div>

关于javascript - 浏览器不会反射(reflect) JS 文件中的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54858357/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com