html如何引用javascript变量

html如何引用javascript变量,第1张

数据库里保存相对路径然后js调用赋值
for(i=0;i=srcslength;i++){
documentimages[i]src=srcs[i];
}
单个赋值的话documentgetElementById('1')src=''

通过HTML的各种Dom事件,比如点击,获取焦点等,这些事件是能绑定JavaScript函数的,在JavaScript函数里可以获取Dom元素比如文本框中的值,再把值赋值给JavaScript文件中的变量即可。

例子:

通过HTML中的输入框(name为spd),修改js中的变量spd:

html文件中的输入框

js文件中的变量,以及变量赋值

<script language="javascript"> //jquery你会吗
$(function() {
var myDate = new Date();
var month=myDategetMonth()+1;//获取当前月份(0-11,0代表1月)
var date=myDategetDate();
$("#yue")html(month);
$("#ri")html(data);
});
</script> <!--这里是html代码-->
<img id="pic" src="img/logopng" /><div id="riqi"><span id="yue"></span><span id="ri"></span>
</div>
不是构造img,一般没用js生成的。
而是你用img做背景,把riqi的div覆盖在上面。图字分离就行了。这是css布局把riqi的index设大点。不懂再问

js不单可以给html添加样式,还可以添加文本、元素以及属性。
以input为例:
input的属性type、value的属性值都可以由JS来设置。下面贴出简单代码:
<<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<input type="button" value="按钮" id="inp"/>
<script>
var oinp=documentgetElementById("inp");//获取需要 *** 作的标签
oinponclick=function(){//建立触发事件的函数
thistype="input";//改变标签的类型
thisvalue="文本框";//改变标签的文本
}
</script>
</body>
</html>
这是最简单的参数改变。
如果还有不明白的请追问。

<span style="font-size:18px;">
<head runat="server">
<title></title>
<script type="text/javascript">
var RestID = 2;//javascript变量
function a() {//javascript函数
windowopen("showNewsaspxid= " + RestID);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<script language="javascript" type="text/javascript">
var str1,str2
str1="fdsgdg dsfdsf china"
str2="武汉市广播电视大学"
documentwrite("显示字符串1:"+str1+"<br>")
documentwrite("显示字符串2:" + str2 + "<br>")
//documentwrite()函数的写法很重要,写好才能正确的读到javascript的参数
<span style="color:#ff6666;">//方式一,打开新窗口,转到新链接
</span> documentwrite(" <a href= 'showNewsaspxid="+RestID+"' style='text-decoration:none; color:Black;' target='_blank'> 目标页 </a> ")
</script>
<span style="color:#ff9966;">//方式二,使用javascript:locationhref,但是不能在新窗口中打开链接
</span> <a href= "javascript:locationhref= 'showNewsaspxid= ' + RestID " style=" text-decoration:none; color:Black;" target="_blank"> 目标页 </a>
<span style="color:#33cc00;">//方式三,使用javascript:open,只能在搜狗浏览器里打开,在IE、火狐、360浏览器里面都打不开,就是说明这个方法不太好
</span> <a href= "javascript:open( 'showNewsaspxid= ' + RestID)" style=" text-decoration:none; color:Black;" target="_blank"> 目标页 </a>
<span style="color:#ff6666;">//方式四使用windowopen但是鼠标为箭头而不是表示链接时的手型,
</span> <a onclick= "windowopen( 'showNewsaspxid= ' + RestID)" style=" text-decoration:none; color:Black;" target="_blank"> 目标页 </a>
<span style="color:#009900;">//方式五调用 javascript函数 ,但是鼠标为箭头而不是表示链接时的手型,
</span>
<a onclick= "javascript:a()" style=" text-decoration:none; color:Black;" target="_blank"> 目标页 </a></form>
</body>
</span>

1如果你这变量a是最后个值
<input type="text" value="" id="abc">
<script>
var abc = documentgetElementById("abc");
abcvalue = 在script中有一个变量a的值
</script>
2如果就只是个变量那他就只能呆在script里了弄不到页面上,script处理一些 *** 作方法用的


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13378098.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-24
下一篇 2023-07-24

发表评论

登录后才能评论

评论列表(0条)

保存