使用jQuery选择未标记的文本 [英] Selecting untagged text with Jquery

查看:89
本文介绍了使用jQuery选择未标记的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下HTML;

<p class="postcontent">
<img src="#">
Some Text
</p>

我只希望隐藏文本. 到目前为止,我有这个;

I want to be able to hide only the text. So far I have this;

  jQuery(function($) {
$(".postcontent").$('[type=text]').hide();
  });

我对html没有任何控制,所以我需要通过Jquery添加它.

I don't have any control over the html so I need to add it by Jquery.

推荐答案

您可以将文本包装在FLY上,然后将其隐藏:

You can wrap the text on the FLY and then hide it:

<p class='postcontent'> <a href=#> dsds </a> aaa </p>

Javascript:

$('.postcontent').contents().filter(function() {
     return this.nodeType == 3; })
             .wrap('<label"> <label />').parent().hide();

JSFiddle

根据评论进行更新:
为什么要创建两个标签?这很容易,因为它有两个文本节点... :

Update based on a comment:
Why does it create two labels? that's easy because it has two text nodes...:

<p class='postcontent'> (!!FirstTextNode!!)
    <a href=#> dsds </a> (!!SecondTextNode!!) aaa 
</p>    

这篇关于使用jQuery选择未标记的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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