2016-09-25 232 views
2

這可能是一個基本的問題,但要怎樣才能在Javascript中的水平線(a.k.a水平線),類似的HTML標籤<hr>?我希望能夠設置它的顏色和厚度,就像<hr>標籤允許我一樣。使用JS畫一條水平線

我有一個網站,我需要做這樣的<script>標籤內(連同一些相關的代碼)。我不能使用<hr>,因爲我的一些用戶沒有啓用JS;所以對於他們來說,<script>標籤內容都不會顯示,但<hr>仍然會顯示(我不希望發生這種情況)。

+2

你有沒有考慮在'


'和CSS'display:none;'? –

+0

@ AdamD.Ruppe:道歉,我其實不知道。你可以補充說,作爲答案? – Sophia111

+0

我用樣品中的幾種技術回答。一般來說,我總是嘗試編寫純粹的,簡單的HTML,它適用於我想要的,然後使用javascript,CSS或noscript元素對其進行自定義,以涵蓋特殊情況。這樣做往往會提供可讀的源代碼和良好的兼容性 - 基本的html適用於幾乎所有人! –

回答

1

處理問題,第一:

我不能用<hr>,因爲我的一些用戶沒有啓用JS;所以對於他們來說,<script>標籤內容都不會顯示,但<hr>仍然會顯示(我不希望發生這種情況)。

可以在JavaScript中的類添加到您的<html>元素(classList如果你不需要IE 9支持):

document.documentElement.className += ' has-js'; 

然後風格你<hr>不會出現,除非JavaScript的支持:現在

<hr class="hey-that’s-my-line" /> 
.hey-that’s-my-line { 
    display: none; 
    /* change its height & colour here */ 
} 

html.has-js .hey-that’s-my-line { 
    display: block; 
} 

,回答你的問題:你可以創建元素和我使用DOM API將它們插入到您的文檔中。這個答案完全覆蓋了一點,但MDN有a good introduction

var hr = document.createElement('hr'); 

document.body.appendChild(hr); // inserts it at the end of <body>; 
           // appendChild() inserts at the end of any node, 
           // generally 

var ref = document.getElementById('reference-point'); 

ref.parentNode.insertBefore(hr, ref); // inserts it before the element 
             // with the id 'reference-point' 
0

,如果你想使用JavaScript的網頁上只是創建一個HR元素,這是如下:

elem = document.createElement("hr"); //this will create a new element 

/* Use setAttribute to define the property and values you'd like give the element */ 

    elem.setAttribute("width", "100px"); 

/* Then you'll need to add the element to the page */ 

document.body.appendChild(elem); 
+0

問題說JS不適用於某些用戶。 – Brad

+0

@Ryan啊,我誤解了這個問題。謝謝。 – Brad

0

您可以使用標準的<hr>元素與一些CSS從無腳本的用戶隱藏。

看看這個:http://arsdnet.net/testo.html有和沒有JavaScript。下面的代碼:

<!DOCTYPE html> 
<html> 
<head> 
     <title>Noscript example</title> 
     <style> 
       /* this css is used by people with and without javascript */ 
       /* a <link> tag would work as well, of course */ 
     </style> 
     <noscript> 
       <style> 
         /* this is only visible by people without JS enabled */ 
         hr { 
           display: none; 
         } 
       </style> 
     </noscript> 
     <script> 
       /* And this script only runs with JS (obviously) so by adding 
        a class name to the root element (<html>), we can also detect 
        it in CSS.... 
       */ 
       document.documentElement.className += " with-js"; 
     </script> 
     <style> 
       /* ...meaning we can target it with CSS like this too: */ 
       html.with-js hr { 
         color: red; 
       } 
     </style> 
</head> 
<body> 
     Hi, before hr 
     <hr /> 
     After hr 
</body> 
</html> 

<noscript>標籤是有效的HTML從waaaaay回來作品無處不在。它內部的任何內容僅對沒有腳本支持的人員可見,並且可以包含任何內容:備用html,鏈接標記,樣式標記,任何內容。

通過在其中添加樣式標籤,我可以使用CSS隱藏不希望非腳本用戶可以看到的元素。

那麼我只需在它下面寫一個普通的HTML,這對所有的用戶來說都是可見的 - 沒有什麼特別的。然後,使用<noscript><style>我將它修改爲沒有腳本的人作爲特例。

有時順便說一句,你想只顯示東西腳本的用戶,而不是隻隱藏nonscript用戶的東西。最簡單的方法是使用javascript在html元素上粘貼一個類,然後使用CSS來定位該類名稱後代。樣本中接下來的幾行顯示。再次,您編寫普通的HTML,然後在事實後修改它,但這次使用<script>的組合來添加一個類並將<style>作爲目標。

所以用JS看這個頁面,你會看到一個紅色的小時。沒有JS,hr不會出現。