javascript - 无法使用 replaceWith() 将值绑定(bind)到工具提示

标签 javascript jquery html angularjs

我使用 jQuery .replaceWith() 插入新的 DOM 内容。所有内容都将被替换,但 title 的值不绑定(bind)。

我试过下面的代码,但工具提示只显示 {{descriptions.title}} 而不是应该显示的文本。

这是我的代码:

$scope.showDescription = function(descriptions) {
            $("#description").find('span#header').replaceWith("<span id='header' class='mleft' title='{{descriptions.title}}'>" + descriptions.name + " - " + descriptions.title + "</span>");
}

我希望工具提示显示 descriptions.title 值而不仅仅是 {{descriptions.title}}

最佳答案

您只需添加字符串 {{description.title}} 作为您的标题,您需要添加变量本身:

$scope.showDescription = function(descriptions) {
            $("#description").find('span#header').replaceWith("<span id='header' class='mleft' title='" + description.title + "'>" + descriptions.name + " - " + descriptions.title + "</span>");
}

编辑: 在我看来更好的方法是不在这里使用 replaceWith 而只是更改所需的属性:

    $scope.showDescription = function(descriptions) {
                $("#description").find('span#header').attr("title",description.title).html(descriptions.name + descriptions.title);
}

这样你就可以改变html本身而不需要适配JS。

关于javascript - 无法使用 replaceWith() 将值绑定(bind)到工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56897366/

相关文章:

javascript - 如何在手机上滚动一页高度?

javascript - 使用 Firebase 和 AngularJS 从 Facebook 检索公开个人资料

javascript - 如何将 javascript 变量值传递给 php 变量?

php - 为什么在yii框架中使用Chtml

javascript - 使用传单标记在单击事件时添加文本到文档

javascript - 如何添加带有独立 router.js 文件的 Vuetify 选项卡?

html - 在 wordpress 中添加帖子时,帖子出现在页面顶部而不是我想要的内容下方

html - 如何将div放在不同的行中而不占用每行的整个宽度?

javascript - Jquery 脚本看不到 2 个 div 之间的区别

javascript - 想要仅在 daterangepicker 中禁用 'from' 日期字段中的 future 日期