在jquery中从li获取ul Id

时间:2016-03-01 13:03:10

标签: jquery

如何从li ID获取父<ul id='ulTask'> <li id='liSubTask1'>subTask1</li> <li id='liSubTask2'>subTask2</li> <li id='liSubTask3'>subTask3</li> <li id='liSubTask4'>subTask4</li> <li id='liSubTask5'>subTask5</li> </ul> 的父ID?

private String rad;

public String getLottorad() {

    Random r = new Random();
    int[] vek = new int[10];
    for (int i = 0; i < 10; i++) {
        vek[i] = r.nextInt(22);
        rad = String.valueOf(vek[i]);
    }
    return rad;
}

public static void main(String[] args) {
    System.out.print("How many lottery lines do you want? :");
    Scanner tgb = new Scanner(System.in);
    int antal = tgb.nextInt();
    Lab111 l = new Lab111();
    for (int i = 0; i < antal; i++)
        System.out.println(l.getLottorad());

}

2 个答案:

答案 0 :(得分:0)

我希望它可以帮助你...

$('#liSubTask1').parent().id();

答案 1 :(得分:0)

您可以使用$('li').parent().attr('id')获取父元素的ID。

<强>样本

alert($('li').parent().attr('id'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id='ulTask'>    
    <li id='liSubTask1'>subTask1</li>    
    <li id='liSubTask2'>subTask2</li>    
    <li id='liSubTask3'>subTask3</li>   
    <li id='liSubTask4'>subTask4</li>    
    <li id='liSubTask5'>subTask5</li>  
</ul>