相关疑难解决方法(0)

动态创建元素的事件绑定?

我有一些代码,我循环遍历页面上的所有选择框,并将.hover事件绑定到它们,以便在它们的宽度上做一些麻烦mouse on/off.

这发生在页面准备就绪并且工作得很好.

我遇到的问题是,我在初始循环后通过Ajax或DOM添加的任何选择框都不会受到事件限制.

我找到了这个插件(jQuery Live Query Plugin),但在我用插件添加另外5k到我的页面之前,我想知道是否有人知道这样做的方法,无论是直接使用jQuery还是通过其他选项.

javascript jquery events unobtrusive-javascript

1677
推荐指数
21
解决办法
79万
查看次数

live()替代on()不工作jQuery 1.9

我有代码做ajax方法加载新内容
但我有新内容的问题,链接没有应用我做了像
preventDefault和ajax方法,只是在加载新内容
点击链接后,使页面重新加载就像有没有ajax代码
在JQ 1.8中使用live()方法工作,但在更新jQuery之后,没有按照on()方法工作

旧代码工作正常,没有任何问题

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script>
function loadContent(url){
        $.ajax({
            url: url,
            dataType: 'html',
            cache: false
        }).done(function(html){
            var $html = $(html);

            $('title').html($html.filter('title').text());
            $('#main').replaceWith($html.find('#main'));
            $('.nav-menu').replaceWith($html.find('.nav-menu'));
            $('.nav-below').replaceWith($html.find('.nav-below'));
        });
}

$(function(){

    // Get The Content Action
    $('.nav-menu li a,#nav-below a,#main a').live('click',function(e) {
        href = $(this).attr("href");

        loadContent(href);

        history.pushState('', 'New URL: '+href, href);
        e.preventDefault();

        // go top after showing the content
        $('html, body').animate({scrollTop:0}, 'slow');
    });

    // THIS EVENT MAKES SURE THAT THE BACK/FORWARD BUTTONS WORK AS WELL
    window.onpopstate = function(event){
        loadContent(location.pathname);
        $('html, …
Run Code Online (Sandbox Code Playgroud)

javascript ajax jquery

5
推荐指数
1
解决办法
2万
查看次数

清除并附加html后,单击"不工作"

我清除后点击不工作,并使用jquery将html附加到div中.这是html代码

<div id="divMain">
</div>
<input id="btn" type="button" value="Clear&Add"/>
Run Code Online (Sandbox Code Playgroud)

这是jQuery代码

var a = $('<a/>').attr({'id':'aH','href':'#'}).text('Hello');
a.click(function(){
    alert('hello');
});
$('#divMain').append(a);



$('#btn').click(function(){
   var newA = $('#aH');
    $('#divMain').html('');
    $('#divMain').append(newA);
});
Run Code Online (Sandbox Code Playgroud)

这是jsfiddle

只需单击小提琴中的警报链接,它就会显示警告Clear&Add.现在单击按钮.现在单击警报.它不起作用.

html javascript css jquery

2
推荐指数
1
解决办法
561
查看次数

Jquery 事件不会为使用innerHTML 在javascript 中创建的按钮触发

我在网上搜索,但不能做这项工作..

我在 js.file 中创建了一个对话框。在这个js中,我有一个创建按钮的函数,但是使用innerHTML ...

    var divButtons = document.getElementById('buttons_insert1');    
    var buttons_insert = "<button id='okcad' class='button_ok' value='' > <img src='/php/Images/OK.png' alt='OK' /> OK </button>";
    divButtons.innerHTML = divButtons.innerHTML + buttons_insert;
Run Code Online (Sandbox Code Playgroud)

在 index.html 中,我有一个 jquery 函数来获取点击事件。

$("#okcad1").click(function() 
{
   alert('hello world');
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用……该事件不会触发……

有人可以帮助我吗?

问候!

拉斐尔 S。

javascript jquery

1
推荐指数
1
解决办法
1378
查看次数

无法在jquery中获取动态生成按钮的id

可能重复:
jQuery单击不适用于动态创建的项目

        $(document).ready(function(){
            $('#btn').click(function(){

                var createInput=$("<input type='button'>");
                createInput.attr("id","close1");
                createInput.attr("value","close");
                $('#outer-div').append(createInput);
            });

            $("#close1").click(function(){
                alert('hi');
            });
        });


    </script>
</head>
<body>
    <div id='outer-div'>
        <input id='btn' type='button' value='click here'/>
    </div>
Run Code Online (Sandbox Code Playgroud)

代码有什么问题.当我点击名为close的动态生成按钮时,我没有收到警报.请帮忙

jquery

0
推荐指数
1
解决办法
863
查看次数

AJAX 请求后链接不起作用

我正在制作的网页遇到了一个小但相当烦人的问题。因此,我有一个链接,当单击该链接时,它会向 PHP 脚本发出 AJAX 请求(如果重要的话,该链接会发送一个隐藏的输入值)。它工作正常并且按我的预期工作,但是如果我尝试再次单击该链接,它不会执行任何操作。我必须刷新页面才能使其再次工作,但我宁愿不必这样做。

关联: <a name='delete-link' href='#'>Delete</a>

JavaScript:

    $("a[name='delete-link']").click(function(e){
        e.preventDefault();
        var ID = $(this).parents("ul").find("input[name='delete-identifier']").val();

        $.ajax({
            type: 'POST', 
            url: '../ajax.php',
            data: {delete: 'true', delete_id: ID},
            success: function(response){
                $("#result").html(response).fadeIn(500).delay(5000).fadeOut(500);
                $("div[name='update']").load("test.php #update");
            }
        })
    })
Run Code Online (Sandbox Code Playgroud)

#update div 中的唯一代码是这个 PHP 代码,它回显显示用户的表:

<?php
    if ($rs){
        $user_table = "<table class='table table-bordered table-condensed table-hover table-striped'><th>Action</th>";
        $user_table .= "<tr><th>Name</th><th>Privilege ID</th></tr>";
        while ($user = $rs->fetch_array(MYSQL_ASSOC)){
            $user_table .= "<td>". $rank['RankName'] ."</td><td>". $user['PrivilegeID'] ."</td><td>";
            $user_table .= "<td><div class='btn-group'>
                                  <button type='button' class='btn btn-default dropdown-toggle' data-toggle='dropdown' aria-expanded='false'>
                                    Action <span class='caret'></span>
                                  </button> …
Run Code Online (Sandbox Code Playgroud)

html javascript ajax jquery

0
推荐指数
1
解决办法
3016
查看次数

标签 统计

jquery ×6

javascript ×5

ajax ×2

html ×2

css ×1

events ×1

unobtrusive-javascript ×1