簡體   English   中英

如何設置 document.write() 中存在的標簽的樣式?

[英]How to style the tags present in document.write()?

我想設置 document.write() 中存在的 img 標簽的樣式。 怎么做?

我嘗試使用 img{height:100px; width:100px;} 在 head 中的 style 標簽內,但我認為它不是這樣工作的。

     <form name="myform" style="margin-left: 45%">
     <input type="radio" name="ward" value="Thane">Thane</input><br>
     <input type="radio" name="ward" value="Vashi">Vashi</input><br>
     <input type="radio" name="ward" value="Andheri">Andheri</input><br>
     <input type="radio" name="ward" value="Bandra">Bandra</input><br>
     <input type="radio" name="ward" value="Dadar">Dadar</input><br>
     <button onclick="display()" id="submit">Submit</button>
     </form>

      Above code is inside body of html and below code is inside script

      if(document.myform.ward.value=="Thane")
      {
        document.write("Thank You!<br>You have selected Thane");
         document.write("<img src='voted.gif'></img>");
       }

我希望只有當用戶選擇 Thane 時才能顯示 Thane 的圖像和感謝消息。 但我無法為圖像設置樣式。 怎么做? 我不能使用innerHTML,因為它顯示在我選擇選項的同一頁面上,而我不想要那樣。 我是初學者,所以不知道該怎么做。 謝謝你。

雖然通常不推薦這樣做,但您可以像這樣使用內聯樣式:

document.write("<img src='voted.gif' style='height:100px; width:100px;'>");

您不必關閉 img 標簽,因為它是一個元素。

您是否嘗試過通過例如document.getElementsByTagName("some_tag_name")獲取元素

使用getElementsByTagName您可以獲得一個元素,使用一個元素您可以使用樣式對象並獲取或設置元素的屬性。

document.getElementsByTagName("some_tag_name").style.some_style_propertie = "some_value_of_propertie";

一個例子:

<!DOCTYPE html>
<html>
<body>

<div id="myDiv">This is a div.</div>
<br>
<button type="button" onclick="myFunction()">Set solid border</button>

<script>
function myFunction() {
  document.getElementById("myDiv").style.borderStyle = "solid";
}
</script>

</body>
</html>

樣式對象 Javascript 鏈接: https : //www.w3schools.com/jsref/dom_obj_style.asp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM