使用get()jquery从HTML变量中获取HTML标签 [英] Grab Html Tags from HTML Variable using get() jquery

查看:130
本文介绍了使用get()jquery从HTML变量中获取HTML标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先:我是javascript的新手,所以如果我的语法是新手,请原谅我.

First: I am new to javascript so please forgive me if my syntax is novice.

我一直在尝试使用jquery中的get()获取HTML标记中的数据.但是,无论我尝试多少种不同的变体,它都不会起作用.有人可以告诉我我在做什么错吗?

I have been trying to get data in HTML tags using get() in jquery. But no matter how many different variations I try, it just won't work. Can someone please show me what I am doing wrong?

下面是我正在使用的javascript.

Below is the javascript I am using.

$.get('load.html', function(raw){
    var bodyHtml = $(raw).filter('body').html();
    var divHtml = $(raw).filter('div').html();
    var title = $(raw).filter('title').text();
    var tag = $(raw).filter('tag').html();
    $('#body').html(bodyHtml);
    $('#tag').html(tag); 
    $('#title').html(title);
    $('#result').html(divHtml);
    $('#raw').html(raw);
});

下面是load.html页面上的html.

Below is the html on the load.html page.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>title load.html</title>
</head>
<body>
<div>div 1<tag>divtag</tag></div>
<tag>tag</tag>
</body>
</html>

这是页面运行后的结果. index.html

This is the results once page has run. index.html

<html><head>
<title>index.html</title>
<script type="text/javascript" src="js/jquery/jquery-1.5.1.min.js"></script>  

<script type="text/javascript">  

$.get('load.html', function(raw){
    var bodyHtml = $(raw).filter('body').html();
    var divHtml = $(raw).filter('div').html();
    var title = $(raw).filter('title').text();
    var tag = $(raw).filter('tag').html();
    $('#body').html(bodyHtml);
    $('#tag').html(tag); 
    $('#title').html(title);
    $('#result').html(divHtml);
    $('#raw').html(raw);
});

</script> 
</head>

<body> 

<div id="title">title load.html</div>
<div id="body"></div>
<div id="result">div 1<tag>divtag</tag></div>
<div id="tag">tag</div>
<div id="raw">

      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <title>title load.html</title>


      <div>div 1<tag>divtag</tag></div>
      <tag>tag</tag>


</div>


</body></html>

需要注意的一件事.看起来好像不是从jquery发送回来的.另外,如果页面上有多个.该脚本不应该将其设置为数组(例如)

One thing to note. It looks like the isn't sent back from jquery. Also if there is multiple on the page. Shouldn't this script make it a array (example being )

推荐答案

以下是结果.我以为是html()标记剥离了主体,并将head标记剔除出了变量.当我从网址执行警报(原始)时,它将显示完整的html页面.但是,当我尝试修改该变量时,标记()将消失.然后我以为是jquery.所以我尝试了一个正则表达式.果然,我得到了相同的准确结果.因此,我只能检索标题,div之类的标签,而不能检索正文.

Here is the results. I thought it was the html() tag stripping the body, and head tags out of the variable. When I would do a alert(raw) from a url it would show the full html page. But then when I try and modify that variable, the tags () would disappear. I then thought it was jquery. So I tried a regular expression. Sure enough I got the same exact results. So I am only able to retrieve tags like title, divs, but not the body.

$(document).ready(function() { 
    $.get('load.html', function(raw){

        var matches = raw.match(/<title\b[^>]*>(.*?)<\/title>/gi);
        //var matches = matches[0].replace(/(<\/?[^>]+>)/gi, ''); // Strip HTML tags?

        alert(matches);

    }, "html");
}); 

这篇关于使用get()jquery从HTML变量中获取HTML标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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