如何在jQuery函数中使用nth-child

时间:2018-10-05 08:25:39

标签: jquery html

我在这个网站上工作,我在第7列和所有行中都有一个带有输入字段的表

我的问题是我希望第6列和第8列响应我在输入字段中写的内容。

当我在“ Bronzealder ”到“ Kolonitiden ”字段中进行写操作时,我甚至想操纵在输入字段中写的行之前和之后的行。 strong>”,从“ Industritiden ”开始,我想操作之前和之后的2行

我有一个网站的视频。但是到目前为止,无论我使用什么输入字段,我的代码都只处理表的前两行。

https://www.youtube.com/watch?v=gPKySob5f74

我正在尝试达到相同的效果,但是要达到其他比例:

https://forgeinfo.com/?l=tools&s=fairhandel

这里有人可以帮助我解决我的问题吗?

如果我尝试编辑

"$("input").keyup(function(){"

与此:

$("input:nth-child(1)").keyup(function(){

它不会工作:(

<script>
                $(function(){
                    $("input").keyup(function(){
                        var value = $(this).val();
                        $("table tr:nth-child(3) td:nth-child(6)").html(value / 2);
                        $("table tr:nth-child(3) td:nth-child(8)").html("1:2");
                        $("table tr:nth-child(2) td:nth-child(6)").html(value);
                        $("table tr:nth-child(2) td:nth-child(8)").html("1:1");
                        if (value == ''){
                            $("table tr:nth-child(3) td:nth-child(6)").html("");
                            $("table tr:nth-child(3) td:nth-child(8)").html("");
                            $("table tr:nth-child(2) td:nth-child(8)").html("");
                            
                        }
                    });
                });
            </script>
<table ID="tradeTable" class="tableTradeContent">
                    <tr>
                        <th>Tidsalder</th>
                        <th>Billeder</th>
                        <th>Varer</th>
                        <th>Bedre lavere alternativ</th>
                        <th>Bedre højere alternativ</th>
                        <th>Resultat</th>
                        <th>Antal</th>
                        <th>Ratio</th>
                        <th>pris / 24 timer</th>
                    </tr>
                    <tr>
                        <td>Bronzealder</td>
                        <td><img src="img/BA/marmor.png"><img src="img/BA/tømmer.png"><img src="img/BA/farvestoffer.png"><img src="img/BA/sten.png"><img src="img/BA/vin.png"></td>
                        <td>Marmor, Tømmer, Farvestoffer, Sten, Vin</td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td><input ID="input"></td>
                        <td></td>
                        <td>400/400</td>
                    </tr>
                    <tr>
                        <td>Jernalder</td>
                        <td><img src="img/JA/stof.png"><img src="img/JA/ibenholt.png"><img src="img/JA/smykker.png"><img src="img/JA/jern.png"><img src="img/JA/kalksten.png"></td>
                        <td>Stof, Ibenholt, Smykker, Jern, Kalksten</td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td><input ID="input"></td>
                        <td></td>
                        <td>800/800</td>
                    </tr>
                    <tr>
                        <td>Tidlig Middelalder</td>
                        <td><img src="img/TM/kobber.png"><img src="img/TM/guld.png"><img src="img/TM/granit.png"><img src="img/TM/honning.png"><img src="img/TM/alabast.png"></td>
                        <td>Kobber, Guld, Granit, Honning, Alabast</td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td><input ID="input"></td>
                        <td></td>
                        <td>1600/1600</td>
                    </tr>
                    <tr>
                        <td>Højmiddelalder</td>
                        <td><img src="img/HM/teglsten.png"><img src="img/HM/glas.png"><img src="img/HM/tørret urt.png"><img src="img/HM/tov.png"><img src="img/HM/salt.png"></td>
                        <td>Teglsten, Glas, Tørret Urt, Tov, Salt</td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td><input ID="input"></td>
                        <td></td>
                        <td>3200/3200</td>
                    </tr>
                    <tr>
                        <td>Senmiddelalder</td>
                        <td><img src="img/SM/basalt.png"><img src="img/SM/messing.png"><img src="img/SM/krudt.png"><img src="img/SM/silke.png"><img src="img/SM/talkumpudder.png"></td>
                        <td>Basalt, Messing, Krudt, Silke, Talkumpudder</td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td><input ID="input"></td>
                        <td></td>
                        <td>6400/6400</td>
                    </tr>
                    <tr>
                        <td>Kolonitiden</td>
                        <td><img src="img/KT/kaffe.png"><img src="img/KT/papir.png"><img src="img/KT/porcelæn.png"><img src="img/KT/tjære.png"><img src="img/KT/metaltråd.png"></td>
                        <td>Kaffe, Papir, Porcelæn, Tjære, Metaltråd</td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td><input ID="input"></td>
                        <td></td>
                        <td>9600/9600</td>
                    </tr>
                    <tr>
                        <td>Industritiden</td>
                        <td><img src="img/IT/koks.png"><img src="img/IT/gødning.png"><img src="img/IT/gummi.png"><img src="img/IT/tekstiler.png"><img src="img/IT/hvalolie.png"></td>
                        <td>Koks, Gødning, Gummi, Tekstiler, Hvalolie</td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td><input ID="input"></td>
                        <td></td>
                        <td>12800/12800</td>
                    </tr>
                    <tr>
                        <td>Den Progressive Æra</td>
                        <td><img src="img/DPÆ/asbest.png"><img src="img/DPÆ/sprængstoffer.png"><img src="img/DPÆ/maskindele.png"><img src="img/DPÆ/benzin.png"><img src="img/DPÆ/blik.png"></td>
                        <td>Asbest, Sprængstoffer, Maskindele, Benzin, Blik</td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td><input ID="input"></td>
                        <td></td>
                        <td>16000/16000</td>
                    </tr>
                    <tr>
                        <td>Moderne Æra</td>
                        <td><img src="img/MÆ/færdigretter.png"><img src="img/MÆ/jernbeton.png"><img src="img/MÆ/smagsstoffer.png"><img src="img/MÆ/luksusmaterialer.png"><img src="img/MÆ/emballage.png"></td>
                        <td>Færdigretter, Jernbeton, Smagsstoffer, Luksusmaterialer, Emballage</td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td><input ID="input"></td>
                        <td></td>
                        <td>19200/19200 (*)</td>
                    </tr>
                    <tr>
                        <td>Den Postmoderne Tidsalder</td>
                        <td><img src="img/DPT/genomdata.png"><img src="img/DPT/industrifilter.png"><img src="img/DPT/genanvendelige ressourcer.png"><img src="img/DPT/halvledere.png"><img src="img/DPT/stål.png"></td>
                        <td>Genondata, Industrifilter, Genanvendelige Ressourcer, Halvledere, Stål</td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td><input ID="input"></td>
                        <td></td>
                        <td>24000/24000 (*)</td>
                    </tr>
                    <tr>
                        <td>Nutiden</td>
                        <td><img src="img/NT/biometriske data.png"><img src="img/NT/elektromagneter.png"><img src="img/NT/gas.png"><img src="img/NT/plastik.png"><img src="img/NT/robotter.png"></td>
                        <td>Biometriske Data, Elektromagneter, Gas, Plastik, Robotter</td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td><input ID="input"></td>
                        <td></td>
                        <td>28800/28800 (*)</td>
                    </tr>
                    <tr>
                        <td>Morgendagen</td>
                        <td><img src="img/MD/ernæringsforskning.png"><img src="img/MD/papirbeton.png"><img src="img/MD/konserveringsmidler.png"><img src="img/MD/intelligente materialer.png"><img src="img/MD/gennemskinneligt beton.png"></td>
                        <td>Ernæringsforskning, Papirbeton, Konserveringsmidler, Intelligente Materialer, Gennemskinneligt Beton</td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td><input ID="input"></td>
                        <td></td>
                        <td>33600/33600 (*)</td>
                    </tr>
                    <tr>
                        <td>Fremtiden</td>
                        <td><img src="img/FT/alger.png"><img src="img/FT/bio-geokemiske data.png"><img src="img/FT/nanopartikler.png"><img src="img/FT/renset vand.png"><img src="img/FT/superleder.png"></td>
                        <td>Alger, Bio-Kemiske Data, Nanopartikler, Renset Vand, Superledere</td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td><input ID="input"></td>
                        <td></td>
                        <td>40000/40000 (*)</td>
                    </tr>
                    <tr>
                        <td>Arktiske Fremtid</td>
                        <td><img src="img/AF/data om kunstig intelligens.png"><img src="img/AF/bioplastik.png"><img src="img/AF/nanotråd.png"><img src="img/AF/papirbatterier.png"><img src="img/AF/bio benzin.png"></td>
                        <td>Data om Kunstig Intelligens, Bioplastik, Nanotråd, Papirbatterier, Bio Benzin</td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td><input ID="input"></td>
                        <td></td>
                        <td>46800/46800 (*)</td>
                    </tr>
                    <tr>
                        <td>Oceanisk Fremtid</td>
                        <td><img src="img/OF/kunstskæl.png"><img src="img/OF/biolys.png"><img src="img/OF/koraller.png"><img src="img/OF/perler.png"><img src="img/OF/plankton.png"></td>
                        <td>Kunstskæl, Biolys, Koraller, Perler, Plankton</td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td><input ID="input"></td>
                        <td></td>
                        <td>51600/51600 (*)</td>
                    </tr>
                    <tr>
                        <td>Virtuel Fremtid</td>
                        <td><img src="img/VF/kryptovaluta.png"><img src="img/VF/datakrystaller.png"><img src="img/VF/guldris.png"><img src="img/VF/nanitter.png"><img src="img/VF/te silke.png"></td>
                        <td>Kryptovaluta, Datakrystaller, Guldris, Nanitter, Te Silke</td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td><input ID="input"></td>
                        <td></td>
                        <td>64000/64000 (*)</td>
                    </tr>
                </table>

1 个答案:

答案 0 :(得分:1)

您可以使用closest()next()prev()查找下一个和上一个tr。 然后选择要使用find()

处理的tds
   $("input").keyup(function(){
       var $currentTr = $(this).closest('tr');
       var $nextTr = $currentTr.next();
       var $prevTr = $currentTr.prev();

       $nextTr.find('td:nth-child(6)').html('xxx');
       //....
   });