0

我正在尝试从我的博客中的链接应用自定义样式。

我需要检查<a>节点是否有直接<img>节点,以便在这种情况下不应用样式,但我不知道该怎么做。

这是我的js函数

function linkify( selector ) {
    var nodes = document.querySelectorAll( selector );
    for( var i = 0, len = nodes.length; i < len; i++ ) {
        var node = nodes[i];
        var child=(node.firstElementChild||node.firstChild);

        if( !node.className || !node.className.match( /roll/g )) {
            // check that not it tag link and 'read more' button link
            // TODO: Check that not have a img node!
            if((node.getAttribute('rel') != 'tag') && (node.getAttribute('class') != 'more-link')) {
                node.className += ' roll';
            node.innerHTML = '<span data-title="'+ node.text +'">' + node.innerHTML + '</span>';
        }
    } } }

jQuery(document).ready(function( $ ) {
    linkify('.post-content p a');

});
4

1 回答 1

2

我看到您在代码块的底部使用 jQuery,所以我会给您一个 jQuery 答案,因为它更简单,如果您需要纯 javascript 解决方案,请告诉我:

if($(node).children('a').length > 0){
     //If true, your node as a child <a> element
}
于 2013-08-24T19:50:55.477 回答