如何在字符串中使用jQuery? [英] How to use jquery in a string?

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

问题描述

使用jquery,如何搜索一串html代码,例如:

Using jquery, how can you search through a string of html code, for example:

<script type="text/javascript">
    alert($('#t','<span id="t">d</span>').html());
</script>

我希望在警报中得到d,但是我没有得到定义...

I expect to get d in the alert, but I get undefined...

有人知道该怎么做吗?

实际上,我使用ajax请求来获取页面的html字符串.如果我然后尝试将字符串粘贴到某个标签中,那么当前页面将变得一团糟.所以我想搜索给定的字符串.

Actually I use an ajax request to get an html string of the page. If I then try to paste the string in some tag, then the current page gets all messed up. So I wanted to search through the given string.

推荐答案

context 参数是jQuery将在 in 内搜索的上下文. IE.它将查看该上下文的子元素,而不是上下文本身.这有效:

The context parameter is the context within which jQuery will search. I.e. it will look at child elements of that context but not at the context itself. This works:

alert($('#t','<span><span id="t">d</span></span>').html());

我不太确定您正在尝试做什么.您已经有了该元素,无需搜索.此示例可能永远不会应用于任何现实问题.

I'm not quite sure what you're trying to do there though. You already have the element, no need to search for it. This example will probably never be applied to any real world problem.

这篇关于如何在字符串中使用jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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