Javascript 隐藏从外部url加载的元素

Javascript 隐藏从外部url加载的元素,javascript,html,css,Javascript,Html,Css,我试图从外部url加载的js小部件中隐藏所有不包含字符串“Marc”的表行 <script type="text/javascript"> id = "ki1120mmvd"; name = "schedules"; document.write(unescape("%3Cscript src='http://healcode.com/javascripts/hc_widget.js' type='text/javascript'%3E%3C/script%3E"));

我试图从外部url加载的js小部件中隐藏所有不包含字符串“Marc”的表行

<script type="text/javascript">
  id = "ki1120mmvd";
  name = "schedules";
  document.write(unescape("%3Cscript src='http://healcode.com/javascripts/hc_widget.js' type='text/javascript'%3E%3C/script%3E"));
</script>

id=“ki1120mmvd”;
name=“附表”;
document.write(unescape(“%3Cscript src=”)http://healcode.com/javascripts/hc_widget.js'type='text/javascript'%3E%3C/script%3E');
我已经能够使用css使用类id或属性隐藏表行中的一些元素,但是看起来我需要js/jquery/php来完全消除这些行,而我发现的一切都不起作用

以下是我获取代码的链接:

以下是我如何使用它:

您可以使用jQuery选择器,然后根据该选择器隐藏信息

  $("div").not(":contains('marc')").hide();

如果要在客户端执行此操作,请尝试JQuery。您可以使用
$。each
方法迭代每个元素并删除不包含“Marc”的元素


:contains
选择器是最佳选择。但是,在您的具体案例中,为了使其发挥作用,还有一些工作要做。在我的测试中,似乎由于动态加载的外部脚本中的一些断开的URL,
jQuery
$
未定义的。解决方法是在加载表(和其他外部脚本)后重新加载
jQuery
API

[更新]

我正在重新发布一个
.html
文件的全部内容,我相信它呈现了正确的结构(对我来说,它在Chrome中工作)。为了实现所需的字体颜色和URL格式,您还需要做一些必要的修改,您可以通过
jQuery
.css()
函数来实现这些修改,如下所述。请注意:

  • 我完全删除了
    标记并
  • 在动态加载外部脚本之后,我正在调用
    标记中的
    waitForFnc
    函数
  • 我很抱歉需要水平滚动来查看完整的
    jQuery
    样式格式,但是它与解决方案并不相关,直到您想要修改它并应用您自己的格式为止

    <html>
        <head>
    
            <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
            <script>
                var count=0;
    
                function waitForFnc(){
                    if(typeof $ != "undefined")
                    {
                        if($("tr.bikram_yoga").length==0){
                           window.setTimeout(waitForFnc,50);
                        }
                        else
                        {
                                $(" div.healcode .header, div.healcode table.schedule tr th, div.healcode table.schedule tr.odd td, div.healcode table.schedule tr.even td").css("background-color","#f4f4f4");
                                $(".location, span.print_text, a.print_version, .mbo_class, span.day_links, .header, span.hc_date_year, div:nth-of-type(10), a[href*='100000210'], a[href*='/13/'], a[href*='100000242'], a[href*='100000201'], a[href*='/142/'], a[href*='100000174'], a[href*='100000229'], a[href*='100000053'],th.trainer").hide();
                                $("div.healcode").css("padding","20px 0 0 20px !important");
                                $("div.healcode span.hc_day").css({"margin-left": "0 !important",
                "text-align": "left !important"});
                                $("div.healcode table.schedule").css("border","none !important");
                                $("table .schedule, tr schedule_header th").css("background-color","#f4f4f4");
                                $("tr.bikram_yoga").not(":contains('Marc')").hide();
                        }
                    }
                    else{
                        if(count==0){
                            var oHead = document.getElementsByTagName('HEAD').item(0);
                            var oScript= document.createElement("script");
                            oScript.type = "text/javascript";
                            oScript.src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js";
                            oHead.appendChild( oScript);
                            count=count+1;
                        }
                        window.setTimeout(waitForFnc,50);
                    }
                }
            </script>
        </head>
        <body>
            <table>
                <tr>
                    <td align="left" valign="top">
                    <div style="background-color: white;">
                    <script type="text/javascript">
                        id = "ki1120mmvd";
                        name = "schedules";
                        document.write(unescape("%3Cscript src='http://healcode.com/javascripts/hc_widget.js' type='text/javascript'%3E%3C/script%3E"));
                        waitForFnc();
                    </script>
                    <noscript class="normal_left_yellow">
                        Please enable Javascript in order to view the class schedule: <a href="http://healcode.com" target="_blank">HealCode</a>
                    </noscript>
                    </div>
                    </td>
                </tr>
            </table>
        </body>
    </html>
    
    
    var计数=0;
    函数waitForFnc(){
    如果(类型$!=“未定义”)
    {
    如果($(“tr.bikram_yoga”)。长度==0){
    设置超时(waitForFnc,50);
    }
    其他的
    {
    $(“div.healcode.header,div.healcode table.schedule tr th,div.healcode table.schedule tr.odd td,div.healcode table.schedule tr.偶数td”).css(“背景色”,“#f4”);
    $(“.location,span.print_text,a.print_version,.mbo_class,span.day_links,.header,span.hc_date_year,div:nth类型(10),a[href*='100000210'],a[href*='100000242'],a[href*='100000201'],a[href*='142/'],a[href*='100000174'],a[href*='100000229'],a[href*='1000000229'],a[href*='1000000242'],a[href*='53];
    $(“div.healcode”).css(“填充”,“20px 0 20px!重要”);
    $(“div.healcode span.hc_day”).css({“左边距”:“0!重要”,
    “文本对齐”:“左!重要”});
    $(“div.healcode table.schedule”).css(“边框”,“无!重要”);
    $(“table.schedule,tr schedule_header th”).css(“背景色”,“#f4”);
    $(“tr.bikram_yoga”).not(“:contains('Marc')”).hide();
    }
    }
    否则{
    如果(计数=0){
    var oHead=document.getElementsByTagName('HEAD')。项(0);
    var oScript=document.createElement(“脚本”);
    oScript.type=“text/javascript”;
    oScript.src=”http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js";
    oHead.appendChild(oScript);
    计数=计数+1;
    }
    设置超时(waitForFnc,50);
    }
    }
    id=“ki1120mmvd”;
    name=“附表”;
    document.write(unescape(“%3Cscript src=”)http://healcode.com/javascripts/hc_widget.js'type='text/javascript'%3E%3C/script%3E');
    waitForFnc();
    请启用Javascript以查看课程计划:
    
    我尝试了你的建议,使用了.bikram_瑜伽课程,但没有成功。@eldereko确实有效。看到这一点了吗?可能是因为您正在外部加载内容,所以上述代码可能会在检索html之前执行,从而忽略隐藏元素@Alex Filipovici在下面的解决方案中与setTimeoutSorry Stokedout一起解决了这个问题,我想删除这个评论。这件事考虑不周。目前学习Javascript和Jquery以及您的反馈有助于了解可能发生的事情@Alex使用setTimeout是处理来自外部URL的影响代码的关键因素。我正在积极探索您的方法。我将课程从.schedule改为.bikram_瑜珈,因此它针对特定的元素,但没有成功。如果可能的话,你能看看我是否遗漏了什么吗?再次感谢你,哟!非常感谢你。我目前正在阅读代码学院教程,可以从外部URL操作代码这一事实令人惊讶。你花时间帮助一个完全陌生的人,这是对生命的肯定。我认为我已经达到了一个技能水平,可以帮助其他人解决更基本的html和css问题。如果我能做som
    <html>
        <head>
    
            <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
            <script>
                var count=0;
    
                function waitForFnc(){
                    if(typeof $ != "undefined")
                    {
                        if($("tr.bikram_yoga").length==0){
                           window.setTimeout(waitForFnc,50);
                        }
                        else
                        {
                                $(" div.healcode .header, div.healcode table.schedule tr th, div.healcode table.schedule tr.odd td, div.healcode table.schedule tr.even td").css("background-color","#f4f4f4");
                                $(".location, span.print_text, a.print_version, .mbo_class, span.day_links, .header, span.hc_date_year, div:nth-of-type(10), a[href*='100000210'], a[href*='/13/'], a[href*='100000242'], a[href*='100000201'], a[href*='/142/'], a[href*='100000174'], a[href*='100000229'], a[href*='100000053'],th.trainer").hide();
                                $("div.healcode").css("padding","20px 0 0 20px !important");
                                $("div.healcode span.hc_day").css({"margin-left": "0 !important",
                "text-align": "left !important"});
                                $("div.healcode table.schedule").css("border","none !important");
                                $("table .schedule, tr schedule_header th").css("background-color","#f4f4f4");
                                $("tr.bikram_yoga").not(":contains('Marc')").hide();
                        }
                    }
                    else{
                        if(count==0){
                            var oHead = document.getElementsByTagName('HEAD').item(0);
                            var oScript= document.createElement("script");
                            oScript.type = "text/javascript";
                            oScript.src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js";
                            oHead.appendChild( oScript);
                            count=count+1;
                        }
                        window.setTimeout(waitForFnc,50);
                    }
                }
            </script>
        </head>
        <body>
            <table>
                <tr>
                    <td align="left" valign="top">
                    <div style="background-color: white;">
                    <script type="text/javascript">
                        id = "ki1120mmvd";
                        name = "schedules";
                        document.write(unescape("%3Cscript src='http://healcode.com/javascripts/hc_widget.js' type='text/javascript'%3E%3C/script%3E"));
                        waitForFnc();
                    </script>
                    <noscript class="normal_left_yellow">
                        Please enable Javascript in order to view the class schedule: <a href="http://healcode.com" target="_blank">HealCode</a>
                    </noscript>
                    </div>
                    </td>
                </tr>
            </table>
        </body>
    </html>