如何获取html标题元素

发布于 2024-12-09 07:57:53 字数 660 浏览 0 评论 0原文

因为我想发送 中包含的文件名部分例如 css & js 文件名,包括其容器文件夹,通过ajax发送到服务器端。
请注意,在给定的示例中, css & js 位于 files 文件夹内。
那么我怎样才能获得 css 和js 文件名,包括它的容器

<html> <head>
  <link rel="stylesheet" href="files/sample.css"/>
  <script type="text/javascript" src="files/jquery.js"> </script>
</head>  

现在我还需要考虑其他事情,sample.CSS

    #myImage {
       background: url('image/lucy.jpg'); 
  /* also i need to get these image file name including details with location */
    }

as i want to send the file names which is included in <head> section e.g. css & js file name including it's container folder to the server side via ajax.
please note that, in given example the css & js are located inside of files folder.
so how can i get the css & js file names including it's container

<html> <head>
  <link rel="stylesheet" href="files/sample.css"/>
  <script type="text/javascript" src="files/jquery.js"> </script>
</head>  

Now i need to consider about other things too, sample.CSS

    #myImage {
       background: url('image/lucy.jpg'); 
  /* also i need to get these image file name including details with location */
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月亮是我掰弯的 2024-12-16 07:57:53

这是为了获取 head 元素中的资源名称(样式表和脚本):

function filterName(path){
    path = path.split('/');
    return path[path.length - 1];
}
var styleSheets = [];
$('head link[rel=stylesheet]').each(function(){
    styleSheets.push(filterName($(this).attr('href'));
});
var scripts = [];
$('head script[src]').each(function(){
    scripts.push(filterName($(this).attr('src'));
});

实际解析这些文件并计算它们的完整路径相当困难。要获取所有图像,您可以考虑遍历所有 dom 元素并检查它们是否通过 css 附加了任何 background-image

function filterBgImage(n){
    var m = n.match(/url\(["'](.*?)["']\)/);
    if(m && m.length == 2)
        return m[1];
    return n;
}
var imgs = [];
$('*')
    .filter(function(){
        var a =  $(this).css('background-image');
        return a != '' && a != 'none';
    })
    .each(function(){
        imgs.push(filterBgImage($(this).css('background-image')));
    });

这种方法的好处是您不需要将相对路径转换为完整路径,因为 jquery 正在为你做这件事。

this is to get the resources names (stylesheets and scripts) in your head element :

function filterName(path){
    path = path.split('/');
    return path[path.length - 1];
}
var styleSheets = [];
$('head link[rel=stylesheet]').each(function(){
    styleSheets.push(filterName($(this).attr('href'));
});
var scripts = [];
$('head script[src]').each(function(){
    scripts.push(filterName($(this).attr('src'));
});

It's rather hard to actual parse these files and compute their fullpath. To get all the images, you may consider to traverse all of the dom elements and check if they have any background-image attached to them through css :

function filterBgImage(n){
    var m = n.match(/url\(["'](.*?)["']\)/);
    if(m && m.length == 2)
        return m[1];
    return n;
}
var imgs = [];
$('*')
    .filter(function(){
        var a =  $(this).css('background-image');
        return a != '' && a != 'none';
    })
    .each(function(){
        imgs.push(filterBgImage($(this).css('background-image')));
    });

The good thing about this approach is that you do not have to transform the relative path into full path, because jquery is doing that for you.

许久 2024-12-16 07:57:53

我不确定你想要做的事情是否可以通过网络浏览器实现 - 但有一些工具可以在没有 -> 的情况下完成它。 http://www.httrack.com/html/overview.html

Im not sure what you want to do is possible from a web browser - but there are tools that will do it without -> http://www.httrack.com/html/overview.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文