如何通过AJAX调用分配文本时获取锚标记文本

时间:2014-05-15 03:38:22

标签: javascript jquery javascript-events

我有一个网页,其中包含每个新闻项目都有'Leave a comment'链接的新闻列表。该网站使用Sitefinity CMS开发。我需要做的是将'Leave a comment'链接文字更改为'Comments(0)'

以下是该方案的屏幕截图: Here http://oi62.tinypic.com/2wn4wgw.jpg

在Sitefinity中,我找不到直接设置来更改链接的文本。所以,我尝试使用jQuery修改文本。

以下是我在Google Chrome 'Leave a comment'链接 INSPECT 时看到的 HTML 代码。

<a href="#commentsWidget" id="ctl00_camaincontentPlaceholder_C001_masterBlogPostsFrontend_ctl00_ctl00_Repeater_ctrl0_commentlink" 
class=" sfcommentsCounterWrp sfcommentsShort" threadkey="f61abde8-0049-628e-b41a-ff0100332b8d_en" 
displaymode="ShortText">Leave a comment</a>

当我使用jQuery执行以下操作时,我只获得空字符串作为输出

jQuery(document).ready(function ($) {
    $('.sfcommentsCounterWrp.sfcommentsShort').each(function () {
        alert(" the anchor text is : " + $(this).text());

    });
});

经过一些研究和调试,我意识到虽然上面的检查元素HTML代码向我显示了HTML中链接的文本,但源代码在锚标记中没有任何文本。

查看源页面上的锚标记的HTML是:

<a href="#commentsWidget" id="ctl00_camaincontentPlaceholder_C001_masterBlogPostsFrontend_ctl00_ctl00_Repeater_ctrl0_commentlink" class=" sfcommentsCounterWrp sfcommentsShort" threadKey="f61abde8-0049-628e-b41a-ff0100332b8d_en" displayMode="ShortText" href="SfCtrlPresentation/#commentsWidget"><span></span></a>

使用来自sitefinity内置控件的AJAX请求插入锚标记的文本,我无法控制。

我的jQuery正在检查document.ready上的文本,此时只有DOM已就绪,而不是所有外部资源内容,这就是它返回空字符串的原因。

这是将“发表评论”文本添加到页面中的位置:

Sys.Application.add_init(function() {
$create(Telerik.Sitefinity.Modules.Comments.Web.UI.Frontend.CommentsCountControlBinder,
 {"commentText":"Comment","commentsText":"Comments","leaveCommentText":"Leave a comment","ninetyNinePlusText":"99+","serviceUrl":"/RestApi/comments-api"}, null, null);
});

我还尝试在Sys.Application.add_load(myfunction);内调用我的jQuery函数,因为它发生在Sys.Application.add_init()之后但仍然是锚文本为空。

其他信息:

1)我尝试在window.onload()中执行我的JS代码,jQuery(document).on("load"),返回空字符串

2)我将JS代码放在一个单独的文件中,我目前在结束body标记之前的页面末尾有它。

3)我尝试将我的JS代码添加到其他地方,例如head标记,但仍然没有帮助。

4)有兴趣直接查看网页的人click here

我想知道您是否对我需要调用我的代码的JQueryjavascript事件有任何建议,以便在将文本放入锚标记后执行?或者完整的页面加载。或者您知道其他任何解决方法吗?

修改 当我尝试在浏览器的控制台中直接从jQuery执行.text()代码时,它会选择文本

1 个答案:

答案 0 :(得分:0)

看来add_init只是要初始化的函数队列。 这未经测试,但是类似的方法可能对您有用。

$(document).ready(function() {
    Sys.Application.add_init(function() {
        $('.sfcommentsCounterWrp.sfcommentsShort').each(function () {
            alert(" the anchor text is : " + $(this).text());
        });
    });
});
相关问题