简体   繁体   English

JavaScript事件处理程序jQuery

[英]JavaScript Event Handler jQuery

I am working on a hand me down project that was written by someone who was clearly better at HTML and JavaScript than myself. 我正在做一个由我自己编写的项目,该项目显然比HTML和JavaScript都比我自己更好。 The html has AJAX links like this: html具有如下的AJAX链接:

<ul class="subNav">
       <li><a link="contacts.html">Contacts</a></li>
       <li><a link="contacts_add.html">Add Contact</a></li>
</ul>

Which I think are handled in this code: 我认为在以下代码中处理:

$('.subNav li a').click(function() {

        var href = $(this).attr('link')

        $('#mainStage').load(href, function() {
            pageLoad();
        })
})

All of the code above works perfectly. 上面的所有代码都可以完美运行。

My problem is I can't seem to recreate this functionality. 我的问题是我似乎无法重新创建此功能。 I am using this HTML: 我正在使用以下HTML:

 <div class="nameTitle colorOne"><a link="contacts_add.html">
      <span class="firstNameField">Contact Name</span>
 </a></div>

and this JavaScript: 和这个JavaScript:

$('.nameTitle').click(function() {
        alert('')
        $('#mainStage').load("contacts_add.html", function() {
            pageLoad();
        })
})

When I click the "nameTitle" class it should load contacts_add.html into the mainStage section of the page but I cannot see anything happen. 当我单击“ nameTitle”类时,它应该将contacts_add.html加载到页面的mainStage部分中,但是我什么也看不见。 I am sure someone fluent with this style of coding could tell me why my event never fires but the earlier code does. 我相信能熟练使用这种编码风格的人可以告诉我为什么我的事件永远不会触发,而早期的代码却能触发。

Thanks in advance, 提前致谢,

You should try altering your code to something like this: 您应该尝试将代码更改为以下形式:

$('.nameTitle').click(function() {

  //This line finds the address to load
  var address = $(this).children("a").attr("link");

  //This line loads the address and then runs the pageLoad function when it has completed
  $('#mainStage').load(address, function() {
    pageLoad();
  });
});

If this doesnt work it may be because the html is loaded dynamically. 如果这不起作用,则可能是因为html是动态加载的。 In this case you need to use .on , see this link here . 在这种情况下,您需要使用.on ,请在此处查看此链接。

In response to Brett's comment below ive put together a jsfiddle showing .on in action here . 在回答下面IVE布雷特的评论放在一起呈现的jsfiddle .on在行动这里 Also i added .preventDefault as this could cause a problem. 我还添加了.preventDefault因为这可能会导致问题。

try with 尝试

$('.nameTitle a').click(function() {

It can be that you're trying with a browser which accepts click only on 'a' tags, albeit they're getting rare hopefully. 可能是您正在尝试使用仅接受“ a”标签点击的浏览器,尽管希望这种情况越来越少。

The 'click' event will never be raised on your div, since there is an inner a element with an href defined. “ click”事件将永远不会在您的div上引发,因为内部有一个定义了href a元素。 Once you click, the event will be raised on the anchor link first which will, by default, redirect to the location specified by the href. 单击后,该事件将首先在锚链接上引发,默认情况下,该链接将重定向到href指定的位置。 In order to get this working, catch the click when it is raised at the a : 为了使此工作正常进行,请在a处引起点击,以抓住它:

$('.nameTitle a').click(function(e) {
    e.preventDefault(); // prevent browser from following href
    alert('');
    $('#mainStage').load("contacts_add.html", function() {
        pageLoad();
    });
});

Demo -- Commented out the load, since that page isn't on this server. 演示 -注释掉负载,因为该页面不在此服务器上。 Also notice I changed your a element to have attribute href instead of link . 还要注意,我将您a元素更改为具有属性href而不是link

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM