我有一个代码可以从另一个网页下载html源代码:
$.get
(
'http://example.com/ex.php?something=0',
function(response){someFunction(response)}
);
然后是它触发的功能:
function someFunction(data)
{
var someArray = [];
$(data).find('.someClass').each(function(loop, item)
{
someArray.push(item);
});
$('tr:eq(1) > td:eq(0)').html(someArray[0]); //yes, I do need all the containers of class none as I'm planning to put each of them in a table cell later
}
到目前为止,它可以正常工作,但是它已放入<container class="someClass">some text</container>
td中,我只希望从中提取一些文本。
我试图去
$('tr:eq(1) > td:eq(0)').html(someArray[0].html());
和
$('tr:eq(1) > td:eq(0)').html(someArray[0].contents());
但是两者都不起作用。我已经尝试过搜索它,但是.contents()是我发现的唯一东西,.html()是我唯一能想到的东西(通常当我选择了这样的选择器时$('selector')
,html()方法可以工作)。
当前,您实际上是用元素替换单元格的内容。如果只需要其内容,则只需获取其内容即可:
function someFunction(data)
{
var someArray = [];
$(data).find('.someClass').each(function(loop, item)
{
someArray.push($(item).contents());
});
$('tr:eq(1) > td:eq(0)').html(someArray[0]); //yes, I do need all the containers of class none as I'm planning to put each of them in a table cell later
}
item.innerHTML
可能也可以正常工作(而不是$(item).contents()
)。
FWIW,map
在这里可能有用:
function someFunction(data)
{
var someArray = $(data).find('.someClass').map(function(loop, item)
{
return $(item).contents();
});
$('tr:eq(1) > td:eq(0)').html(someArray[0]); //yes, I do need all the containers of class none as I'm planning to put each of them in a table cell later
}
这样就构成someArray
了一个jQuery对象,但是您仍然可以通过[0]
诸如此类来访问其内容。如果需要实际的数组,请添加.get()
到末尾:
function someFunction(data)
{
var someArray = $(data).find('.someClass').map(function(loop, item)
{
return $(item).contents();
}).get(); // <=== Here
$('tr:eq(1) > td:eq(0)').html(someArray[0]); //yes, I do need all the containers of class none as I'm planning to put each of them in a table cell later
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句