te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How can I Observe the contents of an 'a' tag - jquery - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I Observe the contents of an 'a' tag - jquery - Stack Overflow

programmeradmin3浏览0评论

I have a blank <a> tag that content is loaded into via an external piece of javascript. I want to observe the <a> and when its content changes perform another task. The content will only ever change once.

Can this be done?

I am using also using jQuery.

Thanks in advance

I have a blank <a> tag that content is loaded into via an external piece of javascript. I want to observe the <a> and when its content changes perform another task. The content will only ever change once.

Can this be done?

I am using also using jQuery.

Thanks in advance

Share Improve this question edited Mar 1, 2011 at 14:27 skaffman 404k96 gold badges824 silver badges775 bronze badges asked Mar 1, 2011 at 13:07 LizardLizard 45k39 gold badges107 silver badges170 bronze badges 4
  • do you want to observe the "content" or the href attribute ? – jAndy Commented Mar 1, 2011 at 13:12
  • 1 nope the actual contents of the <a>OBSERVE HERE</a> – Lizard Commented Mar 1, 2011 at 13:21
  • 1 Wouldn't it just be easier to have your external javascript call a function after it performs the change? – Mottie Commented Mar 1, 2011 at 14:19
  • 1 This JavaScript wasn't written by us. – Lizard Commented Mar 1, 2011 at 19:58
Add a ment  | 

4 Answers 4

Reset to default 6

You can use a mixture out of jQuery && DOM Level 3 events (see browser support below).

If you want to check for any changes within the content, you could do this:

var $a = $('a');

$a.one('DOMNodeInserted', function(e) {
    console.log('content changed!: ', e);    

    console.log('new content: ', $(this).html());   
});

$a.one('DOMAttrModified', function(e) {
    console.log('attribute changed!: ');        

    console.log('attribute that was changed: ', e.attrName);
});

See this code in action: http://jsfiddle/wJbMj/1/

Reference: DOMNodeInserted, DOMAttrModified


While the above solution is actually pretty convinient to me, it'll only work in browser that support those events. To have a more generic solution, you can hook into jQuerys setter methods. The downside in this solution is, that you will only catch changes that were done through jQuery.

var _oldAttr = $.fn.attr;
$.fn.attr = function() {
    console.log('changed attr: ', arguments[0]);
    console.log('new value: ', arguments[1]);
    return _oldAttr.apply(this, arguments);
};

You could hook into .text() and .html() the exact same way. You would need to check if the this value within the overwritten methods represent the correct DOMnode.

You can try monitoring the .html() of the tag to see if it changes to anything else...

Maybe have a timed function (executing every n-seconds) that monitors the content (.html()) of the element until it changes and then stops monitoring it. Maybe something in the vein of:

var monitor = true;
var doMonitor = function() {
  monitor = $("#theanchor").html() != "the initial value";
  if (monitor)
    setTimeout(doMonitor,500);
};
setTimeout(doMonitor,500);

In theory this should work, but I haven't tested it.

You could make use of the DOMSubtreeModified event. That event fires at an element when it's contents change.

$('a').bind('DOMSubtreeModified', function() {
    // contents changed   
});

Note: this event does not fire in Opera and older versions of IE (it works in IE9 though).

Live demo: http://jsfiddle/simevidas/pLvgM/

What do you mean content of an a tag? You can do this like so:

$('#linkElementID').html();

or get an attribute like so:

$('#linkElementID').attr("title"); //Title Attribute 
$('#linkElementID').attr("href"); //href Attribute etc etc 

I don't think there is an event that is fired when a tag changes, the .change() event is only fired by input areas and select lists.

You would need to check after the event that loads the newly inserted content.

发布评论

评论列表(0)

  1. 暂无评论