3

如果用户的浏览器上未启用 JavaScript,是否可以用 HTML 替换 javascript?

我知道我可以使用<noscript>this displays in place of javascript</noscript>

这工作正常,但它仍然运行 javascript。

理论上我想要这个:

如果启用了 javascript,则
  运行 javascript

如果未启用
  javascript,请不要运行 javascript 并提供替代方法

我正在使用这个 jQuery 插件: http: //malsup.com/jquery/cycle/int2.html

当我在 safari 上禁用 javascript 时,它会显示所有三个项目,都在一个 div 中。该插件淡入每个项目,但禁用它会连续显示所有三个项目,而不会像启用 javascript 那样淡入和淡出。

禁用 javascript 后,我​​想阻止它同时显示所有三个项目。我将向您展示它的外观以及禁用 JavaScript 时的作用。

禁用视图: http: //i42.tinypic.com/212y1j6.png(注意它们 3 堆叠在彼此之上) - 我想阻止这种情况发生,因为 JavaScript 被禁用

启用视图:http: //i39.tinypic.com/9gwu3d.png

这是项目所在的 div 的代码:

$(document).ready(function() {
    $('#featured-programs-left').cycle({ 
        fx:     'fade', 
        speed:  'slow',
        timeout: 15000,
        next:   '#next2',
        prev:   '#prev2' 
    });
});

<div id="featured-programs-left">

<div>
    <a href="http://site.com/academics/majors/emergency_medical_technician_-_paramedic/" title="Emergency Medical Technician - Paramedic"><img src="http://site.com/images/uploads/images/emt.jpg" alt="Emergency Medical Technician - Paramedic" /></a>
    <strong>Emergency Medical Technician - Paramedic</strong>
    <p>This unique A.A.S. degree program, a partnership between College and Faxton-St. Luke&#8217;s Healthcare provides the paramedic student the education necessary to function in an</p> 
    <p><a href="http://site.com/academics/majors/emergency_medical_technician_-_paramedic/" title="Learn more about Emergency Medical Technician - Paramedic">Learn more</a></p>
</div>

<div>
    <a href="http://site.com/academics/majors/travel_tourism_hospitality_events_management/" title="Travel &amp; Tourism: Hospitality &amp; Events Management"><img src="http://site.com/images/uploads/images/hospitality_event_planning.jpg" alt="Travel &amp; Tourism: Hospitality &amp; Events Management" /></a>
    <strong>Travel &amp; Tourism: Hospitality &amp; Events Management</strong>
    <p>This program prepares students for exciting careers in the travel and tourism industry and the hospitality and events planning field. Graduates are prepared to:<br</p> 
    <p><a href="http://site.com/academics/majors/travel_tourism_hospitality_events_management/" title="Learn more about Travel &amp; Tourism: Hospitality &amp; Events Management">Learn more</a></p>
</div>

<div>
    <a href="http://site.com/academics/majors/fashion_buying_merchandising/" title="Fashion Buying &amp; Merchandising"><img src="http://site.com/images/uploads/images/fashion_merchandising.jpg" alt="Fashion Buying &amp; Merchandising" /></a>
    <strong>Fashion Buying &amp; Merchandising</strong>
    <p>This program prepares graduates for careers throughout the Fashion Industry including positions in buying, fashion merchandising, retail and wholesale sales, retail</p> 
    <p><a href="http://site.com/academics/majors/fashion_buying_merchandising/" title="Learn more about Fashion Buying &amp; Merchandising">Learn more</a></p>
</div>

</div>

div的CSS

#featured-programs-left img,
#featured-programs-right img{
    xfloat:left;
    overflow:auto;
    xclear:left;
    xwidth:351px;
    xpadding:0 5px 5px 0;
    border:0;
}

#featured-programs-left,
#featured-programs-right {
    height:625px;
    float:left;
    overflow:auto;
    clear:left;
    clear:right;
    width:100%;
    xborder:2px solid red;
}

#featured-programs-left div,
#featured-programs-right div {
    xborder:1px solid purple;
    overflow:auto;
    clear:left;
    clear:right;
    width:352px;
    height:345px;
}

#featured-programs-left {
    float:left;
}
4

3 回答 3

3

使用的替代方法<noscript>是在页面加载后立即使用 JavaScript 隐藏某个元素。如果 JavaScript 被禁用,则元素将保持显示状态。如果启用了 JavaScript,您的脚本将被执行并且元素将被隐藏。

window.onload = function () {
    document.getElementById("no_script").style.display = "none";
}

<div id="no_script">
You don't have JavaScript enabled.
</div>

更新

如果你想做相反的事情(在启用 JavaScript 时显示一些 HTML),你总是可以使用各种方法将新元素注入到 DOM 树中。这是一个:

$(document).ready(function() {
    $('#container').html($('#content').html());
});

<div id="container"></div>
<script type="text/html" id="content">
    <div>Your <em>HTML</em> goes here</div>
</script>

感谢John Resig<script type="text/html">HTML 中隐藏 HTML 模板的技巧。浏览器显然不会执行或呈现<script>非常规类型的内容。

于 2009-01-28T17:24:37.470 回答
3

有点像Ates 的解决方案,您可以使用 Javascript 为启用它的用户更改内容。例如,假设您有一个精美的菜单,它为 Javascript 用户提供了超级简单的导航,但对非 JS 用户毫无价值。在 HTML 中将显示属性设置为“无”,然后使用 JS 启用它。在您的情况下,如果您有不想向非 JS 用户显示的内容,您可以默认隐藏它。缺点是如果浏览器关闭了 JS 和 CSS,这将不起作用。如果您对此感到担心,可以使用 JS 插入内容。

<html>
<head>
  <script>
    $(document).ready(function() {
      $('.jsok').show();
    });
  </script>
  <style>
    .jsok { display: none; }
  </style>
 </head>
 <body>
   <div class="jsok"><!-- content for JS users here--></div>
   <div><!-- content for everyone here --></div>
   <noscript><!-- content for non-js users here --></noscript>
 </body>
 </html>
于 2009-01-28T22:45:02.500 回答
2

如果启用了脚本,它将运行脚本。如果您想暂存 <noscript> 行为,请在浏览器中禁用 JavaScript 并尝试一下。

于 2009-01-28T17:23:22.853 回答