Jquery 获取父节点元素
我需要获取子节点的父节点 这是我当前的代码Jquery 获取父节点元素,jquery,Jquery,我需要获取子节点的父节点 这是我当前的代码 console.log (($(editor.selection.getNode()).parent()[0])); 这个返回像这样的东西 <ol>....</ol> 。。。。 但我需要的是只捕捉Ol元素 所以任何人都可以帮忙吗?如果我理解正确,您正在尝试获取标签名。您可以使用标记名属性从元素中获取标记名。试试这个 console.log (($(editor.selection.getNode()).parent()[0
console.log (($(editor.selection.getNode()).parent()[0]));
这个返回像这样的东西
<ol>....</ol>
。。。。
但我需要的是只捕捉Ol元素
所以任何人都可以帮忙吗?如果我理解正确,您正在尝试获取标签名。您可以使用标记名属性从元素中获取标记名。试试这个
console.log (($(editor.selection.getNode()).parent()[0].tagName));
HTML
工作演示
Reference你能给我们看看你的HTML吗?@Chanmz“我需要的是只捕捉Ol元素”的意思?听起来是一样的
ol element only
和…
现在,您捕获了什么?这似乎很好,当它登录控制台时,它还打印子元素
<ol id="h1">
<li>I</li>
<li>II</li>
</ol>
<ol id="h2">
<li>I</li>
<li>II</li>
</ol>
$(document).ready(function () {
$('li').click(function(){
alert($(this).parent().attr('id'));
$(this).parent().css('background-color', 'red');
});
});