Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中检查子节点_Javascript_Wordpress - Fatal编程技术网

如何在javascript中检查子节点

如何在javascript中检查子节点,javascript,wordpress,Javascript,Wordpress,我正在尝试应用我博客中链接的自定义样式 我需要检查节点是否有一个直接的节点,以便在这种情况下不应用样式,但我不知道如何做 这是我的js函数 function linkify( selector ) { var nodes = document.querySelectorAll( selector ); for( var i = 0, len = nodes.length; i < len; i++ ) { var node = nodes[i];

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

我需要检查
节点是否有一个直接的
节点,以便在这种情况下不应用样式,但我不知道如何做

这是我的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');

});
功能链接(选择器){
var节点=document.querySelectorAll(选择器);
对于(var i=0,len=nodes.length;i
我看到您在代码块的底部使用jQuery,所以我会给您一个jQuery答案,因为它更简单,如果您需要纯javascript解决方案,请告诉我:

if($(node).children('a').length > 0){
     //If true, your node as a child <a> element
}
if($(节点).children('a')。长度>0){
//如果为true,则将节点作为子元素
}

为什么不使用jquery?不完全正确,但谢谢你的想法。我已经使用了
node.children[0]
来检查它。现在,我如何检查子节点是否是
img
标记?我可能误解了,您需要知道节点是否有
img
子节点?