如何使用jQuery获得一些文本 [英] How to get a bit of text using jQuery

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

问题描述

在以下情况下,我需要从特定的id区域获取文本,例如,在这种情况下,我想获取此人的名字.我已经知道了ID,即booking_11,因为我是从onClick那里获得的:

I have the following scenario where I need to get text from a specific id area, for instance, in this case I'd like to get the name of the person. I already know the id i.e. booking_11 as I've got this from the onClick:

<li id="booking_11">Name of person <span style="float: right;"><a href="#" id="11" class="dialog">View</a> | <a class="remove" id="rmCandidate_11" href="#">Remove</a></span></li>

推荐答案

要仅获取 "Name of person"文本节点,您可以执行此操作(无需更改标记):

To get only that "Name of person" text node you can do this (without changing your markup):

var name = $("#booking_11").contents().filter(function() { 
                                           return this.nodeType == 3; 
                                       }).text();

您可以在此处进行测试(上面的代码经过分解以完全适合SO,它可以成为一线客.)

You can test it out here (broken up above to fit SO cleanly, it can be a one-liner).

这篇关于如何使用jQuery获得一些文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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