在元素悬停上调用节点函数

时间:2017-09-03 19:36:00

标签: javascript node.js ejs

我使用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文件

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函数。怎么预防这个?我只想在鼠标输入

上调用它

1 个答案:

答案 0 :(得分:1)

这样做的方法是创建一个REST调用,在这种情况下为GET来检索数据。 您不应该从客户端调用服务器功能。

参见nodejs rest tutorialexpress.js

编辑:

正如您所见here,您需要在事件处理上使用客户端JS。

因此,您必须使用服务器调用来检索该数据。

相关问题