简体   繁体   English

jQuery的不捡HTML

[英]jquery not picking up html

So I have the following HTML 所以我有以下HTML

<div class="tmpl" id="query_tmpl">
<tr>
    <td><a href="#" class="group"></a></td>
    <td class="primary_email"></td>
</tr>
</div>

and the following JS: 和以下JS:

 console.log($('#query_tmpl').html());

For some reason this only logs the 'a' tag. 由于某种原因,这只会记录'a'标签。 (Ex: http://jsfiddle.net/L8RQq/ ) (例如: http : //jsfiddle.net/L8RQq/

Why does this happen and how do I get around it so that I can properly pick up the tr/td? 为什么会发生这种情况,以及如何解决它,以便可以正确接收tr / td? I'm using jQuery 1.9.2 if that makes any difference. 我正在使用jQuery 1.9.2,如果有什么区别。

Update: Yes, the markup is 'bad html', but the whole point of this question is how to get around that. 更新:是的,标记是“错误的html”,但是这个问题的重点是如何解决这个问题。 Using the HTML present and without altering it, how can I grab the contents even though it's 'bad'? 使用现有的HTML而不更改它,即使它很“糟糕”,我如何也可以抓住其中的内容?

You don't have table tags around your tr . 您在tr周围没有table标签。 Try this: 尝试这个:

<div class="tmpl" id="query_tmpl">
    <table>
        <tr>
            <td><a href="#" class="group"></a></td>
            <td class="primary_email"></td>
        </tr>
    </table>
</div>

This will log the div's contents properly . 这将正确记录div的内容

the reason it wasn't logging, is because the browser sees some invalid tr and td tags, and removes those, because they can only be in a table , leaving you with only the a . 之所以没有记录日志,是因为浏览器看到了一些无效的trtd标签,并删除了它们,因为它们只能位于table ,而只剩下a

If you can't change the markup, tell the person / company that wrote that markup to fix it. 如果您无法更改标记,请告诉编写该标记的人员/公司进行修复。 It's invalid HTML. 这是无效的HTML。

Make it like this 像这样

    <table class="tmpl" id="query_tmpl">
         <tr>
            <td><a href="#" class="group"></a></td>
            <td class="primary_email"></td>
         </tr>
    </table>

Then it will surely work correctly 那它一定可以正常工作

如果要拾取tr或td,则可以使用css操作函数,例如children([selector]) ,其中selector最终是一个class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM