[英]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.