代码之家  ›  专栏  ›  技术社区  ›  Tauren

脚本标记的jquery选择器问题

  •  9
  • Tauren  · 技术社区  · 16 年前

    <script type="text/html"> 页面中的标记。我用 <script> how John Resig does it . 出于某种原因,以下jquery选择器似乎没有选择任何内容:

    $("script[type*=html]").each(function() {
        alert("Found script "+this.id);
    });
    

    <body>
        <script id="filter-search" type="text/html">
            <dt>Search</dt>
            <dd><input type="text"/></dd>
        </script>
    </body>
    

    我也试着把它放到HTML文档的头部,但仍然没有找到。从未显示警报。

    $("script[type*=javascript]").each(function() {
        alert("Found script "+this.id);
    });
    

    然后它只会在头脑中找到那些 src

    <head>
        <script type="text/javascript" src="jquery.js" id="jquery"></script> 
        <script type="text/javascript" src="jquery-ui.js" id="ui"></script> 
        <script type="text/javascript" id="custom">
            $(document).ready( function() {
                $("script[type*=javascript]").each(function() {
                    alert("Found script "+this.id);
                });         
                $("script[type*=html]").each(function() {
                    alert("Found TEMPLATE script "+this.id);
                });         
            });
        </script> 
        <script id="filter-test" type="text/html">
            <dt>Test</dt>
        </script>
    </head>
    <body>
        <script id="filter-search" type="text/html">
            <dt>Search</dt>
            <dd><input type="text"/></dd>
        </script>
    </body>
    

    我收到以下警报:

    • 找到脚本jquery

    这个 custom filter-test 头中的脚本不会被选中,头中的脚本也不会被选中 filter-search 中的脚本 body 标签。

    编辑:

    2 回复  |  直到 16 年前
        1
  •  2
  •   majackson    16 年前

    你用什么浏览器?这个脚本在Chrome、Firefox和IE6中运行得很好,给了我一些提示:

    • 找到脚本ui
    • 找到自定义脚本
    • 找到模板脚本筛选器搜索
        2
  •  1
  •   jcl    13 年前

    外部脚本出现的问题可能是由于加载顺序造成的。为了最大限度地提高响应能力,浏览器在页面中遇到脚本时就开始加载和执行脚本,甚至在页面完全下载和解析之前。因此,如果您在head元素中链接了一个脚本,它可能无法访问页面后面指定的DOM部分,比如自定义脚本标记。

    两种可能的解决方案: