Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.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 如何通过jquery选择器选择父元素的子元素?_Javascript_Jquery - Fatal编程技术网

Javascript 如何通过jquery选择器选择父元素的子元素?

Javascript 如何通过jquery选择器选择父元素的子元素?,javascript,jquery,Javascript,Jquery,如何在下面的html代码中从函数tbodyExpand中选择tbody use jquery? 我试着使用$this。最近的“table”。找到“tbody”,但不起作用 <div class="bs-example"> <table class="table table-border table-striped table-condensed table-hover"> <thead> <tr>

如何在下面的html代码中从函数tbodyExpand中选择tbody use jquery? 我试着使用$this。最近的“table”。找到“tbody”,但不起作用

<div class="bs-example">
    <table class="table table-border table-striped table-condensed table-hover">
        <thead>
        <tr>
            <td><b>TITLE</b><a href="javascript:void(0)" onclick="tbodyExpand()" style="float: right;">EXPAND</a></td>
        </tr>
        </thead>
        <tbody class="hide">
        <tr>
            <td>ALT (U/L)</td>
            <td><input class="span1" name="ALT" value="" placeholder=""></td>              
        </tr>
通过执行onclick=tbodyExpand.callthis,您可以使用:

  $(this).closest('table').find('tbody');
否则,该函数内部的对象将指向全局对象窗口,而不是单击的元素

您可以使用选择器绑定事件,而不是编写内联处理程序

例如:

<a href="#" class="expand floatRight">EXPAND</a>

通过这种方式,您还可以分离css、js和html,并避免编写内联脚本/样式。

通过执行onclick=tbodyExpand.call,您可以使用:

  $(this).closest('table').find('tbody');
否则,该函数内部的对象将指向全局对象窗口,而不是单击的元素

您可以使用选择器绑定事件,而不是编写内联处理程序

例如:

<a href="#" class="expand floatRight">EXPAND</a>


通过这种方式,您还可以分离css、js和html,避免编写内联脚本/样式。

尝试类似的方法

html


试试这样的

html


虽然所有这些示例都是合法的,但您可以大大简化您的结构:

给活动元素一个ID在本例中,我已经给了它click

绑定到。单击该ID的事件:

$('#click').click(function(e) {
    //you can do stuff here! and use $(this).
});
搜索所需的元素:

$this.closest'table.找到'tbody'

导致:

<div class="bs-example">
    <table class="table table-border table-striped table-condensed table-hover">
        <thead>
            <tr>
                <td><b>CLICK EXPAND:</b> <a href="#" id='click' style="float: right;">EXPAND</a>
                </td>
            </tr>
        </thead>
        <tbody class="hide">
            <tr>
                <td>ALT (U/L)</td>
                <td>
                    <input class="span1" name="ALT" value="" placeholder="" />
                </td>
            </tr>
        </tbody>
    </table>
</div>
以及加载jQuery的javascript:

$('#click').click(function(e) {
    e.preventDefault(); //blocks the default action of an <a> link.
    console.log($(this).closest('table').find('tbody'));
});
请注意,我稍微修改了您的代码,以便更容易地查看发生了什么


在这里使用它

虽然所有这些示例都是合法的,但您可以大大简化您的结构:

给活动元素一个ID在本例中,我已经给了它click

绑定到。单击该ID的事件:

$('#click').click(function(e) {
    //you can do stuff here! and use $(this).
});
搜索所需的元素:

$this.closest'table.找到'tbody'

导致:

<div class="bs-example">
    <table class="table table-border table-striped table-condensed table-hover">
        <thead>
            <tr>
                <td><b>CLICK EXPAND:</b> <a href="#" id='click' style="float: right;">EXPAND</a>
                </td>
            </tr>
        </thead>
        <tbody class="hide">
            <tr>
                <td>ALT (U/L)</td>
                <td>
                    <input class="span1" name="ALT" value="" placeholder="" />
                </td>
            </tr>
        </tbody>
    </table>
</div>
以及加载jQuery的javascript:

$('#click').click(function(e) {
    e.preventDefault(); //blocks the default action of an <a> link.
    console.log($(this).closest('table').find('tbody'));
});
请注意,我稍微修改了您的代码,以便更容易地查看发生了什么

在这里玩转它

什么是$this引用?请看下面的评论:为什么首先要将inline onclick与jQuery结合起来?只需使用jQuery何谓$this引用?请参阅以下注释:为什么首先要将内联onclick与jQuery相结合?只需使用jQuery