使用jQuery获取样式标记内容? [英] Get style tag content with jQuery?

查看:76
本文介绍了使用jQuery获取样式标记内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我正在尝试在样式标记中获取 但我无法让它发挥作用。

I'm trying to get the content within the style tags but I can't get it to work.

代码的作用

我使用jQuery并尝试使用text()和html()获取样式内容。它不起作用。

I use jQuery and try to get the style content with text() and html(). It does not work.

我的代码

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet/less" type="text/css" href="css/style.less">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" type="text/javascript"></script>

    <style id="less:concepts-less-css-style" media="screen" type="text/css">
        body {
            padding: 10px;
            background: blue;
        }
    </style>

</head>
<body>
<script src="js/less-1.1.5.min.js" type="text/javascript"></script>
<script type="text/javascript">
        jQuery(document).ready(function($) {        
            var my_id = $('style').attr('id');
            var test = $('#less:concepts-less-css-style').text();
            var test2 = $('#less:concepts-less-css-style').html();
            alert(test + "#" + test2 + "#" + my_id);
        });
    </script>
</body>
</html>


推荐答案

我认为你必须逃避你的冒号角色ID,因为它通常意味着选择器语法中的某些内容。请参阅在元素ID中处理冒号CSS选择器,用于讨论此问题。

I think you have to escape the colon character in your ID because it would normally mean something in the selector syntax. See Handling a colon in an element ID in a CSS selector for a discussion of this issue.

$('#less\\:concepts-less-css-style')

我个人在我的ID或类值中避免使用带有CSS选择器的特殊字符避免这种复杂化。

I personally avoid special characters with CSS selector meaning in my ID or class values to avoid this complication.

这篇关于使用jQuery获取样式标记内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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