Javascript Onclick仅重新加载div

Javascript Onclick仅重新加载div,javascript,jquery,html,twitter-bootstrap,Javascript,Jquery,Html,Twitter Bootstrap,$(窗口).load(函数(){ $(“#按钮”)。单击(函数(){ 警报('单击') $(“#div”).load(“#div>*”); }); }); 刷新 × 神圣的鳄梨酱 × 神圣的鳄梨酱 × 神圣的鳄梨酱 对于jQuery版本3,函数加载和事件加载的处理方式不同 因此,您需要在代码中编写: $(window).on('load', function (e) { 有关详细信息,请参阅: 已删除不推荐使用的事件别名 .load、.unload和.error(自jQuery 1.8以来已

$(窗口).load(函数(){
$(“#按钮”)。单击(函数(){
警报('单击')
$(“#div”).load(“#div>*”);
});
});

刷新
×
神圣的鳄梨酱
×
神圣的鳄梨酱
×
神圣的鳄梨酱

对于jQuery版本3,函数加载和事件加载的处理方式不同

因此,您需要在代码中编写:

$(window).on('load', function (e) {
有关详细信息,请参阅:

已删除不推荐使用的事件别名

.load、.unload和.error(自jQuery 1.8以来已弃用)已不再存在。使用.on()注册侦听器

因为BootstrapV3与jQueryV3不兼容(请参阅),所以我在代码段中更改为jQuery1.x

我的片段:

$(窗口).on('load',函数(e){
$('#MyButton')。在('单击')上,函数(e){
警报('单击')
$(“#div”).load(“#div>*”);
});
});

刷新
×
神圣的鳄梨酱
×
神圣的鳄梨酱
×
神圣的鳄梨酱

加载方法从服务器请求一些内容,并将其放在您调用它的div中。 其使用的一个例子可以是:

$("#IdOfElementToPutContentIn").load("urlToGrabDataFrom")
查看API以更好地理解这一点:


在本地计算机上尝试此代码。它在我的浏览器中工作

<html>
    <head>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
        <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    </head>
    <body>

        <button id="MyButton" class="btn btn-warning">Refresh</button>
        <div class="col-md-3" id="div">

            <div class="alert alert-warning alert-dismissible fade in" role="alert">
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
                <strong>Holy guacamole!</strong>
            </div>
            <div class="alert alert-danger alert-dismissible fade in" role="alert">
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
                <strong>Holy guacamole!</strong>
            </div>
            <div class="alert alert-success alert-dismissible fade in" role="alert">
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
                <strong>Holy guacamole!</strong>

            </div>
        </div>
        <script type="text/javascript">
        $(window).on('load', function (e) {
            $('#MyButton').on('click', function (e) {
                alert('clicked')
                $("#div").load(" #div > *");
            });
        });
        </script>
    </body>
</html>

刷新
×
神圣的鳄梨酱
×
神圣的鳄梨酱
×
神圣的鳄梨酱
$(窗口).on('load',函数(e){
$('#MyButton')。在('单击')上,函数(e){
警报('单击')
$(“#div”).load(“#div>*”);
});
});

像这样使用$(“#MyButton”)。单击(function(),你的htmlI中没有按钮id我认为它在那里@deepRefresh抱歉,我修改了,忘记更新,现在将更新…您可能有用户旧版本(它不工作,但在控制台中它会触发错误,因为
jquery.min.js:4 XMLHttpRequest无法加载。(html链接)跨源请求仅支持协议方案:http、data、chrome、chrome extension、https、chrome extension resource。
@RiotZeastCaptain for bootstrap 3.3.6您不能使用jQuery 3.x。因此,我将代码片段更新为jQuery 1.x。请立即尝试并告知我。