Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 搜索变量以查找标记_Jquery_Loops_Variables_Tags_Selector - Fatal编程技术网

Jquery 搜索变量以查找标记

Jquery 搜索变量以查找标记,jquery,loops,variables,tags,selector,Jquery,Loops,Variables,Tags,Selector,我有一些通过ajax调用获得的html。我正在将html字符串分配给一个变量。我想做的是搜索这个变量,而不是整个文档,寻找预标记并显示文本。正确的方法是什么 $(document).ready(function () { var rawtext = htmlstringwithpretags $(rawtext).find('pre').each(function () { alert($(this).text()); }); }); 试试这个 html

我有一些通过ajax调用获得的html。我正在将html字符串分配给一个变量。我想做的是搜索这个变量,而不是整个文档,寻找预标记并显示文本。正确的方法是什么

$(document).ready(function () {
    var rawtext = htmlstringwithpretags
    $(rawtext).find('pre').each(function () {
        alert($(this).text());
    });
});
试试这个

htmlstring
有一个
pre
标记

var rawtext = "hello wold<pre>pre text content</pre>";
alert($("<div>"+rawtext+"</div>").find('pre').text());
试试这个:

var rawtext = "hello wold<pre>pre text content</pre>";
var html = $.parseHTML(rawtext);

$.each(html, function(i, el){
    if( 'PRE' == el.nodeName ){ alert(el) }
});
$(“”+rawtext+“”)。查找(“pre”)。每个(函数(){
警报($(this.text());
});

在这种情况下,您可能需要查看

var rawtext=“hello woldpre text content”;
var html=$.parseHTML(rawtext);
$.each(html、函数(i、el){
如果('PRE'==el.nodeName){alert(el)}
});

你能给我们举个例子吗?使用
substring()
indexOf()
你的实现有什么问题吗?@code jaff问题是我想对变量而不是标记执行搜索。它只有在正文中的div中有文本时才起作用。我无法将html直接转储到身体中。MS中的某些安全性阻止了我这样做。@Jotter checkout my answer这是在为变量创建新的div吗?我可以在这里使用.each进行迭代吗?你的意思是,htmlstring有多个pre标记,你想得到所有的值吗?就是这样。我将警报更改为:警报($(el).text())};因为我需要文本。
$("<div>"+rawtext+"</div>").find("pre").each(function(){
    alert($(this).text());
});
var rawtext = "hello wold<pre>pre text content</pre>";
var html = $.parseHTML(rawtext);

$.each(html, function(i, el){
    if( 'PRE' == el.nodeName ){ alert(el) }
});