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 に答える