简体   繁体   English

渲染局部视图。 Javascript文件不适用于该渲染视图吗?

[英]Rendering a partial view. Javascript files dont apply to that rendered view?

I am using nodejs. 我正在使用nodejs。 And calling something like the following from a javascript file: 并从javascript文件中调用类似以下内容的内容:

$("#right").append(resp);

I then initialize it as a datatable but all the functions (like doubleclick) defined on datatables don;t work. 然后,我将其初始化为数据表,但在数据表上定义的所有功能(如doubleclick)均无效。 If I copy the code for the doubleclick into the console it works. 如果我将双击的代码复制到控制台中,它将起作用。

What am I doing wrong? 我究竟做错了什么?

Edit: more info. 编辑:更多信息。 So what I have now is when a user logs in it displays their playlists in a sidebar using ajax. 因此,我现在所拥有的就是当用户登录时使用ajax在边栏中显示其播放列表。 When the playlist is clicked it loads that playlists. 单击播放列表后,它将加载该播放列表。 However, the code in application.js(loaded initially) does not apply to the playlists unless the page is fully reloaded. 但是,除非页面完全重新加载,否则application.js(最初加载)中的代码不适用于播放列表。

Here is some code I have: 这是我有一些代码:

    $.ajax({
  type: "POST",
  url: '/login',
  data: {  email: email, password: pass },
  success: function(result) {
    $.get('/toop', function(data) {
    console.log(data);
    if (data == "fail") {
      alert("error");
    } else {
    $("#header").html(data);
    $(document).trigger('close.facebox');
    $.get('/getPlaylists' , function(playlists) {
    $("#left").html(playlists);
    });

This just gets the playlists for the users and puts them on the sidebar. 这只是为用户获取播放列表,并将其放在侧边栏上。 Now in the application.js file I have: 现在在application.js文件中,我有:

    $(".user-playlist").click(function(e) {
    var playlist = e.target;
    var id = $(playlist).attr('data-playlist');

. . . .

But it only detects a click of the playlist if I do a full refresh of the page. 但是,如果我完全刷新页面,它只会检测到播放列表的点击。 Just rendering it using ajax doesnt. 仅仅使用ajax渲染就没有。 I am doing {layout: false} when getting the new view. 获取新视图时,我正在执行{layout:false}。 Do I have to reload application.js? 我是否需要重新加载application.js? I am new to node (and web dev). 我是Node(和Web开发人员)的新手。 Thanks 谢谢

The click event handler will only be applied to the set of elements which match .user-playlist when $(".user-playlist") is called. 当调用$(".user-playlist")时, click事件处理程序将仅应用于与.user-playlist匹配的元素集。 To see which elements those are, open the web inspector (or Firebug) and add this line just before the JavaScript snippet you included in your post: 要查看哪些元素,请打开Web检查器(或Firebug),并在您帖子中包含的JavaScript代码段之前添加以下行:

console.log($(".user-playlist"))

You should see an empty array on the page where the playlist is loaded from Ajax, and at least one item on the page after a full refresh. 您应该在从Ajax加载播放列表的页面上看到一个空数组,并在完全刷新后在页面上看到至少一个项目。

To attach the event handler even for elements which are not on the page yet, read the documentation for the on method (specifically, the functionality that used to belong to the live method). 要甚至为页面上尚未存在的元素附加事件处理程序,请阅读on方法的文档(具体来说,该方法曾经属于live方法)。

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

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