没有父表标签的td [英] td without parent table tag

查看:62
本文介绍了没有父表标签的td的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTML:

<td class="tabletd"> text one </td><br>
<td class="tabletd" id="tdSecond"> this is next td</td>
<button onclick="myFunc()">click</button>

JS:

function myFunc() {
    var second    = document.getElementById('tdSecond').innerHTML;
    //var second2 = document.getElementsByTagName('td')[1]; //gives error undefined
    alert(second);
}

我无法完成这项工作.甚至CSS也没有影响,请参见 JSFiddle .
但是,如果我们使用<li>而没有其父级<ul>,则javascript和CSS都可以正常工作.
并且javascript和CSS在自定义标签上也可以正常工作.例如:-<ddd> text </ddd>

I cannot make this work. Even there is no effect of css see JSFiddle.
But both javascript and css work fine if we use <li> without its parent <ul>.
And also javascript and css work fine on custom tag. for eg:- <ddd> text </ddd>

那么,如果我们使用<td>而没有其父级<table>的话,为什么在CSS和javascript上都出现错误?

So why we get error on both css and javascript if we use <td> without its parent <table> ?

推荐答案

如果您检查chrome中的元素,您会注意到代码已更改为以下内容:

If you inspect the element in chrome you will notice the code has changed to the following:

<body>
    text one <br>
    this is next td
    <button onClick="myFunc()">click</button>
    <script type="text/javascript">//<![CDATA[ 

    function myFunc() {
    var second=document.getElementById('tdSecond').innerHTML;
    alert(second);

    //var second2=document.getElementsByTagName('td')[1].innerHTML;
    //alert(second2);  
    }

    //]]>  

    </script>
</body>

td元素已被浏览器删除,因为它们无效.

The td elements have been stripped by the browser as they are not valid.

这篇关于没有父表标签的td的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆