2013-06-27 51 views
-1

我需要的部份子节点的父节点获取父节点元素

这是我当前的代码

console.log (($(editor.selection.getNode()).parent()[0])); 

这将返回类似这样的

<ol>....</ol> 

但什么一些事情我需要的只是赶上Ol元素

所以任何人都可以帮忙?

+1

你能告诉我们你的HTML请求吗? – krishgopinath

+0

@Chanmz“我需要的只是赶上Ol元素”的意思? – Praveen

+0

听起来像是一样的东西......'ol元素只有'和'

    ...
' – MikeM

回答

0

如果我理解正确,您正在尝试获取标记名称。您可以使用tagName属性从元素中获取标签名称。试试这个

console.log (($(editor.selection.getNode()).parent()[0].tagName)); 
+0

感谢Tamillharasan – ChamZ

0

HTML

<ol id="h1"> 
    <li>I</li> 
    <li>II</li> 
</ol> 
<ol id="h2"> 
    <li>I</li> 
    <li>II</li> 
</ol> 

jQuery的

$(document).ready(function() { 
    $('li').click(function(){ 
     alert($(this).parent().attr('id')); 
     $(this).parent().css('background-color', 'red'); 
    }); 
}); 

工作演示http://jsfiddle.net/SB659/

参考http://api.jquery.com/parent/