相关疑难解决方法(0)

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

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

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

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

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

javascript jquery events unobtrusive-javascript

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

jQuery如何绑定onclick事件以动态添加HTML元素

我想将onclick事件绑定到我使用jQuery动态插入的元素

但它永远不会运行绑定功能.如果你能指出为什么这个例子不起作用以及如何让它正常运行,我会很高兴:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"        
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="da" lang="da">
        <head>
          <title>test of click binding</title>

<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
          <script type="text/javascript">


        jQuery(function(){
          close_link = $('<a class="" href="#">Click here to see an alert</a>');
          close_link.bind("click", function(){
            alert('hello from binded function call');
            //do stuff here...
          });
  
          $('.add_to_this').append(close_link);
        });
          </script>
        </head>
        <body>
          <h1 >Test of click binding</h1>
          <p>problem: to bind a click event to an element I append via JQuery.</p>

          <div class="add_to_this">
            <p>The link is created, then added here below:</p>
          </div>

          <div …
Run Code Online (Sandbox Code Playgroud)

javascript jquery bind

123
推荐指数
4
解决办法
24万
查看次数

在jQuery中绑定动态创建的元素

以下代码是相当自我解释的,但我遇到了将click事件绑定到已创建的元素的问题.

你可以在第25行看到我正在创建一个id为'overlay'的div.然后我设置它的css属性.

然后在第65行我绑定点击以触发隐藏模态.问题是,它不起作用.如果我把div放在html中,.click就会按预期工作.

任何帮助表示赞赏.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Modal</title>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
<script type="text/javascript">

$(document).ready(function() {  

    // Select the link(s) with name=modal
    $('a[name=modal]').click(function(e) {

        // Cancel the link behavior
        e.preventDefault();

        // Get the id of this link's associated content box.
        var id = $(this).attr('href');

        // Find the screen height and width
        var overlayHeight = $(document).height();
        var overlayWidth = $(window).width();

        // Create the overlay
        $('body').append('<div id="overlay"></div>');

        //Set css properties for newly created #overlay
        $('#overlay').css({ …
Run Code Online (Sandbox Code Playgroud)

javascript jquery

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