繁体   English   中英

隐藏从外部网址加载的元素

[英]Hide an element loaded from external url

我正在尝试从外部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或属性使用CSS来隐藏表行中的某些元素,但看起来我需要js / jquery / php才能完全消除行,而我发现的所有内容都行不通。

这是我获取代码的链接: http : //www.funkydoor.com/studio_polk_street.html

这是我的使用方式: http : //www.marcmatisyoga.com/schedule.html

您可以使用:contains() jQuery选择器,然后根据该信息隐藏信息。

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

快速演示

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

$(document).ready(function() {
    $('.schedule tr').each(function(index){
         if ($(this).html().indexOf('Marc') === -1)
             $(this).hide();
    });
});

:contains选择器是理想的选择。 但是,在您的特定情况下,需要做更多的工作才能使它起作用。 在我的测试中,由于外部脚本中动态加载的URL损坏, jQuery$ undefined 解决方法是在表(和其他外部脚本)加载后重新加载jQuery API。

[更新]

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

  1. 我完全删除了<style>...</style>标记,
  2. 我正在动态加载外部脚本之后,在<body><script>标记中调用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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM