Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.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自动获取::before元素的信息_Javascript_Html_Css_Html Table - Fatal编程技术网

使用JavaScript自动获取::before元素的信息

使用JavaScript自动获取::before元素的信息,javascript,html,css,html-table,Javascript,Html,Css,Html Table,我一直在试图找出一个等待,让它在一个网页上有多个表 <table class="table table-striped basic"> <thead> <tr> <th>Saturday</th> <th class="time">10:30am</th> <th class="time">11:00am&

我一直在试图找出一个等待,让它在一个网页上有多个表

<table class="table table-striped basic">
    <thead>
        <tr>
            <th>Saturday</th>
            <th class="time">10:30am</th>
            <th class="time">11:00am</th>
            <th class="time">11:30am</th>
            <th class="time">12:00pm</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>3/5/2016</td>
            <td>15231</td>
            <td>15232</td>
            <td>15233</td>
            <td>15234</td>
        </tr>
        <tr>
            <td>3/5/2016</td>
            <td>15231</td>
            <td>15232</td>
            <td>15233</td>
            <td>15234</td>
        </tr>
试图找出一种方法来获取每个“th”的上下文,并将其::before放置在CSS中,同时具有多个表。有什么建议吗?

var-time=$(“.time”)
返回一个类为
.time
的所有元素的所有文本。相反,应该在for循环中使用类似于
$('.time:nth child('+i+')).text()的内容

<script>
    var time = $('.time');
    $(function() {
      for(var i = 0, len=time.length; i<len; i++){
        $('table td:nth-child(' +i+ ')').attr('data-before-content', $(time[i]).text());
      }
    });

</script>

变量时间=$('.time');
$(函数(){
for(var i=0,len=time.length;i
var time=$('.time');
返回类
.time
中所有元素的所有文本。相反,应该在for循环中使用类似于
$('.time:n个子('+i+')).text()的内容

<script>
    var time = $('.time');
    $(function() {
      for(var i = 0, len=time.length; i<len; i++){
        $('table td:nth-child(' +i+ ')').attr('data-before-content', $(time[i]).text());
      }
    });

</script>

变量时间=$('.time');
$(函数(){

对于(var i=0,len=time.length;i您是否在考虑类似的问题?您可以使用
.each()
.eq()
.not()

$('table tr')。每个(函数(){
$('td',this).not('first child')。每个(函数(i){
$(this.attr('data-before-content',$('.time').eq(i.text());
});
});
。表td:before{
内容:attr(内容之前的数据)'\00a0';
}
.表td{
填充:1em;
}

星期六
上午10:30
上午11:00
上午11:30
下午12:00
3/5/2016
15231
15232
15233
15234
3/5/2016
15231
15232
15233
15234

您是否在考虑类似的问题?您可以使用
.each()
.eq()
.not()

$('table tr')。每个(函数(){
$('td',this).not('first child')。每个(函数(i){
$(this.attr('data-before-content',$('.time').eq(i.text());
});
});
。表td:before{
内容:attr(内容之前的数据)'\00a0';
}
.表td{
填充:1em;
}

星期六
上午10:30
上午11:00
上午11:30
下午12:00
3/5/2016
15231
15232
15233
15234
3/5/2016
15231
15232
15233
15234

这似乎可行,我必须在同一页上有两个表,其中包含不同的信息来测试。谢谢,这至少给了我一个起点。不客气。很高兴我能提供帮助。只是好奇,但这两个表的“时间”不同吗数据?是的,这就是问题所在,但除非你有任何建议,否则很容易改变。@DanielRubio我更新了我的答案。为了澄清,我添加了
.closest()的用法
搜索DOM树直到找到匹配项。在这种情况下,它搜索最近的表祖先节点,然后在下面搜索
。时间
。这似乎起作用了,我必须在同一页上有两个表,信息不同来测试它。谢谢,这至少给了我一个起点。不客气。很高兴我可能会有帮助。只是好奇,但这两个表是否有不同的“时间”数据?是的,这就是问题发生的地方,但除非您有任何建议,否则它可以很容易地更改。@DanielRubio我更新了我的答案。为了澄清,我添加了
.closest()的用法
向上搜索DOM树,直到找到匹配项。在本例中,它搜索最近的表祖先节点,然后在该节点下方搜索
。time
<script>
    var time = $('.time');
    $(function() {
      for(var i = 0, len=time.length; i<len; i++){
        $('table td:nth-child(' +i+ ')').attr('data-before-content', $(time[i]).text());
      }
    });

</script>