2017-09-03 168 views
0

我使用EJS模板來顯示文章列表,我想徘徊用戶圖片呼叫功能點徘徊

模板

<%for (var i=0; i< articles.length; i++){%> 
    <div class="ui-block list-article"> 
     <article class="hentry post video"> 
      <div class="post__author author vcard inline-items"> 
       <div id="userProfilImage" class="userProfilImage"> 
        <img src="<%= articles[i].user.image %>" alt="author" /> 
       </div> 
      </div> 
     </article> 
    </div> 
<% } %> 

時的NodeJS調用節點功能與當前文章爲PARAM文件

app.locals.getUserPopupInfo = function(article){ 
    article.user.name= "blablabla"; 
} 

如何徘徊用戶圖像就像當調用app.locals.getUserPopupInfo功能:

<div id="userProfilImage" class="userProfilImage" onmouseenter="getUserPopupInfo(articles[i])"> 
    <img src="<%= articles[i].user.image %>" alt="author" /> 
</div> 

感謝

編輯:

我發現這一點:

<div id="userProfilImage" class="userProfilImage" onmouseenter="<%= getUserPopupInfo(articles[i]) %>"> 
    <img src="<%= articles[i].user.image %>" alt="author" /> 
</div> 

但它呼籲在頁面加載getUserPopupInfo功能。如何防止這一點?我想打電話給它只在鼠標上輸入

+0

在頁面加載時調用Ejs注入。這是整個想法。 – Itamar

回答

1

這樣做的方法是創建一個REST調用,GET在這種情況下檢索數據。 您不應該從客戶端調用服務器功能。

nodejs rest tutorial 或者express.js

編輯:

正如你可以看到here,你需要在事件處理使用客戶端JS。

因此,您必須使用服務器調用來檢索該數據。

+0

我在這個答案中看到https://stackoverflow.com/a/13294515/6002163這是可能的。你能給我舉個例子嗎? –

+0

試着用<%%> – Itamar

+0

來包圍函數調用我已經更新了我的答案,你可以看一下嗎? –