2017-05-21 32 views
0

我想使用Js來打擊文本,但它不能通過使用Js來完成。JavaScript罷工()操作不起作用

<!DOCTYPE html> 
<html> 

    <head> 
    <script type="text/javascript"> 
     document.getElementsByTagName("p")[0].innerText.strike(); 

    </script> 

    </head> 

    <body> 

    <p>Version 2.0 is not yet available! now available!</p> 

    <p>The strike element is not supported in HTML5. Use CSS instead.</p> 

    </body> 
</html> 

我甚至試過innerHTML,但它仍然無法正常工作。

回答

2

to be deprecated strike不會在原地

工作一般需要重新分配和屬性的值回他們的JavaScript對象。

在這種情況下,你需要分配的innerHTML了刪除線的文本,因爲它只是在
<strike></strike>標籤

var par = document.getElementsByTagName("p")[0]; 
 
par.innerHTML = par.innerText.strike();
<p>Version 2.0 is not yet available! now available!</p> 
 

 
<p>The strike element is not supported in HTML5. Use CSS instead.</p>

更好的包裝使用CSS:text-decoration: line-through;

var par = document.getElementsByTagName("p")[0]; 
 
par.classList.add("strike");
.strike { text-decoration: line-through }
<p>Version 2.0 is not yet available! now available!</p> 
 

 
<p>The strike element is not supported in HTML5. Use CSS instead.</p>

+0

它運作良好,非常感謝。 – androidnewbie