0

我正在尝试确保已加载 jQuery。我正在尝试通过设置它来测试它,以便当您单击一个按钮时,该按钮会被删除。它不工作。有人可以向我指出我在这里做错了什么吗?

此外,我正在尝试使按钮居中。我正在使用的 CSS 不起作用。我相信我可以通过一点努力自己解决这个问题,但如果有人能解释一下如何做会很好。

HTML:

<!DOCTYPE html>
<html>
<head>
    <link type="text/css" rel="stylesheet" href="chaos.css"/>
    <script type="text/javascript" src="Chaos.js"></script>
    <script type="text/javascript" src="jquery-1.10.0.min.js"></script>
    <title>MTG Chaos Roller</title>
</head>
<body>
    <div id="buttons">
        <input type="submit" value="Roll Chaos">
        <input type="submit" value="Roll EnchantWorldLand">
        <input type="submit" value="Roll PersonaLand">
        <input type="submit" value="Roll WackyLand">
    </div>

</body>
</html>

CSS:

body {
    background-color: black
};

#buttons {
    margin-left:auto;
    margin-right:auto;
};

JS:

$(document).ready(function(){
    $(#buttons).click(function(){

    });

    $(document).on(function('click', '#buttons', .remove(this)){

    });
});

哦,还有:有没有办法保持这个网站上代码的格式,而不必在每行的开头添加四个空格?说明并没有完全解释它。

4

2 回答 2

2

你几乎明白了:

$(document).on('click', '#buttons', function() {
    $(this).remove();
});

在您的第一个电话中,您忘记了引号:

$("#buttons").click(function(){
于 2013-05-25T02:51:50.173 回答
2

错误的

    $(#buttons).click(function(){

    });

正确的:-

$('#buttons').click(function(){
        $(this).remove();
        });

错误的

 $(document).on(function('click', '#buttons', .remove(this)){

    });

正确的

$(document).on('click', '#buttons', function(){
        $(this).remove();
})

事件委托使用on主要用于动态创建的元素,以便将事件附加到document head容器或任何容器,以便将其委托给现在预设的目标元素或将来动态添加。如果您知道它是否已经存在于 DOM 中并且在以后的一段时间内没有更改或没有动态添加,请不要使用它。你的第一个选择会很好用。

可能你的意思是:-

$('#buttons input').click(function () {
      $(this).remove();
});

更新

您需要在 JS 之前先加载 jquery(假设您的 js 使用 jquery)

<head>
    <link type="text/css" rel="stylesheet" href="chaos.css"/>
    <script type="text/javascript" src="jquery-1.10.0.min.js"></script>
    <script type="text/javascript" src="Chaos.js"></script>

    <title>MTG Chaos Roller</title>
</head>
于 2013-05-25T02:52:54.083 回答