如何检查内容的长度?

时间:2019-12-28 23:29:42

标签: javascript

嗨,我正在尝试创建购物车。我想做的是单击“完成”按钮,这样我可以删去文本。

    var shoppingList = document.getElementsByClassName("shoppingList");
    if (shoppingList.str.length > 0) {
        style.textDecorationLine = "line-through";
    }
        else {
            return false;
        }
}

是我在javascript文件上所做的,

<head>
    <title>Javascript + DOM</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <h1>Shopping List</h1>
    <p id="first">Get it done today</p>
    <input id="userinput" type="text" placeholder="enter items">
    <button id="enter">Enter</button>
    <ul>
        <li class="bold red" random="23"><a class="shoppingList" href="">Notebook</a><button id="done" type="button" >Done</button><button class="remove" type="button">Remove</button></li>
        <li><a class="shoppingList" href="">Jello</a><button id="done" type="button">Done</button><button class="remove" type="button">Remove</button></li>
        <li><a class="shoppingList" href="">Spinach</a><button id="done" type="button">Done</button><button class="remove" type="button">Remove</button></li>
        <li><a class="shoppingList" href="">Rice</a><button id="done" type="button">Done</button><button class="remove" type="button">Remove</button></li>
        <li><a class="shoppingList" href="">Birthday Cake</a><button id="done" type="button">Done</button><button class="remove" type="button">Remove</button></li>
        <li><a class="shoppingList" href="">Candles</a><button id="done" type="button">Done</button><button class="remove" type="button">Remove</button></li>
    </ul>
    <script type="text/javascript" src="script.js"></script>
</body>
</html>

是我放在HTML文件中的内容。 我认为style.textDecorationLine = "line-through"之前应该有一些东西 但我不确定应该是什么。

1 个答案:

答案 0 :(得分:0)

style.textDecorationLine = "line-through"之前的内容是包含所选元素的变量,例如:

document.querySelector(".elementToCancel").style.textDecorationLine = "line-through";

OR

document.querySelector(".elementToCancel").style.textDecoration = "line-through";