【问题标题】:Call node function on element hover在元素悬停时调用节点函数
【发布时间】:2017-09-06 17:55:04
【问题描述】:

我正在使用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 函数。如何防止这种情况?我只想在鼠标输入时调用它

【问题讨论】:

  • 在页面加载时调用 Ejs 注入。这就是整个想法。

标签: javascript node.js ejs


【解决方案1】:

执行此操作的方法是创建一个 REST 调用,在本例中为 GET 以检索数据。 你不应该从客户端调用服务器函数。

nodejs rest tutorialexpress.js

编辑:

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

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

【讨论】:

猜你喜欢
  • 2021-11-18
  • 1970-01-01
  • 2018-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-10
  • 1970-01-01
  • 2021-05-24
相关资源
最近更新 更多