为什么我的javascript代码不能在html代码中工作?
为什么我的javascript代码不能在html代码中工作为什么我的javascript代码不能在html代码中工作?,javascript,html,Javascript,Html,为什么我的javascript代码不能在html代码中工作 它不起作用,因为您不能在另一个标记的开始标记中启动标记 相反,您需要使用一个脚本标记,并让它输出整个img标记,而不仅仅是它的一个属性: <script> if (condition) { document.write('<img with one thing in it...>'); } else { document.write('<img with another thing in
它不起作用,因为您不能在另一个标记的开始标记中启动标记
相反,您需要使用一个脚本标记,并让它输出整个img
标记,而不仅仅是它的一个属性:
<script>
if (condition) {
document.write('<img with one thing in it...>');
} else {
document.write('<img with another thing in it...>');
}
</script>
这种情况永远不会是真的。同样,你也有
var IntNum = 1
if (IntNum == 1) {
document.writeln("yes")
} else {
document.writeln("no")
}
这个条件永远不会是错误的。Javascript代码应该是它自己的独立元素,而不是嵌入在
alt
或title
属性中的东西
如果要使用Javascript代码更改alt
或title
属性,则需要在其他标记之外正确指定代码,然后让它操作DOM,搜索并更改相关属性,例如:
<html><head><script>
function fn() {
document.title = "Changed title";
}
</script></head><body>
<a href="javascript:fn()">Click here</a>
</body></html>
函数fn(){
document.title=“更改的标题”;
}
我的朋友,这绝对不是如何使用js的
浏览器将仅将脚本作为alt和title属性读取,而不会作为脚本执行
根据您的代码,您可能会尝试实现以下目标:
<img class="transparent" id="img-id" src="http://ecample.com/images/1.png" width="16" height="16">
<script>
var image=document.getElementById('img-id');
var IntNum = 0
if( IntNum == 1 ){
image.alt="yes";
image.title="yes";
}else{
image.alt="no";
image.title="no";
}
</script>
var image=document.getElementById('img-id');
var IntNum=0
if(IntNum==1){
image.alt=“是”;
image.title=“是”;
}否则{
image.alt=“否”;
image.title=“否”;
}
但是我不知道你从哪里得到IntNum的值,它会一直是0值。jsin
alt
tag?这不是它的工作原理<代码>它不起作用从来都不是问题的好描述。它有什么作用?你期待什么?你想用这个代码做什么?选民们:仅仅因为它不是这样工作的,这并不意味着一个问题问为什么是一个糟糕的问题。看着这伤害了我的眼睛,你的回答非常好。谢谢
<img class="transparent" id="img-id" src="http://ecample.com/images/1.png" width="16" height="16">
<script>
var image=document.getElementById('img-id');
var IntNum = 0
if( IntNum == 1 ){
image.alt="yes";
image.title="yes";
}else{
image.alt="no";
image.title="no";
}
</script>