简体   繁体   English

在没有页面加载的情况下运行javascript

[英]Run javascript without page load

I have a box with two dropdownlist . 我有一个带有两个下拉列表的框。 box content change when second dropdown change. 第二个下拉菜单更改时,框内容也会更改。 box contain ul with lot of li , i want when clicked on li get li content . 盒子里有很多李,包括我,我想当点击李时得到李的内容。 this do for first time when page load , but after box content change , my script not run. 这是第一次在页面加载时执行,但是在框内容更改后,我的脚本无法运行。


this is my code (when dropdown change and box content change): 这是我的代码(当下拉列表更改和框内容更改时):

 $.ajax({url: '/Symfony/web/app_dev.php/linknews?s='+target+'&&c='+select,
           success: function(output) {
              var list = document.getElementById(news_list);
              var line = document.getElementById("line");
              $(list).empty(); 

          var newss = JSON.parse(output);
          newss.forEach(function(entry){

            var link = document.createElement("a");
            var linkcontent = document.createTextNode(entry["title"]);
            link.appendChild(linkcontent);
            link.title = entry["title"];
            link.href = entry["address"];

            var div = document.createElement("div");
            div.innerHTML += '&nbsp';
            div.setAttribute("class" , "news-bottom");

            var li = document.createElement("li");
            li.appendChild(link);
            li.appendChild(div);

            list.appendChild(li);
            $(line).show();
          });     
       }
});

and code that show to me li content: 和向我展示li内容的代码:

    $(document).ready(function($)
      $("#news-list li a").click(function() {
      var name = this.getAttribute('href');
      alert(name);
}));

i don't know when box content change , why my script not run?! 我不知道框内容何时更改,为什么我的脚本无法运行?

My question have two aspect: 我的问题有两个方面:

first: Cross-browser onload event and the Back button , and answer: cross-browser 首先:跨浏览器的onload事件和“返回”按钮,然后回答: 跨浏览器

second: I should use on instead of click: $(document).ready(function(){ 第二:我应该使用on而不是单击:$(document).ready(function(){

$('#news-list').on('click','a',function(){

    alert($(this).attr('href')); // Get the ref attribute from the "a" element
});
$(window).bind("unload", function() {});

}); });

attention that : Method on was introduced in jQuery version 1.7. 注意: jQuery版本1.7中引入了on方法。

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

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