我在html所在的文件夹中定义了一个gongju.js文件,代码如下:
var gongju = new array(9);
gongju[0]="工具.html";
gongju[1].src="工具-字符-康熙字典.html";
.......
然后再html中进行了调用
<script></script>
请问,我该怎么在li中的 a href处调用gongju[1]的路径呢?
康熙字典
修改下,GongJu.js的代码是这样的:
var GongJu = new Array(9);
GongJu[0]="工具.html";
GongJu[1]="工具-字符-康熙字典.html";
为什么不在a标签的href里面直接写“工具-字符-康熙字典.html”呢?
如果你想动态添加,那么就在js里面给a标签的href属性加上链接,或者是a标签加上click事件。
回复2L 其实最关键的是不想到时候修改文件路径的时候,改很多地方。 到时候只要改JS文件的路径就好。
通过js控制 增加li的事件
<script type="text/javascript" src="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script><script type="text/javascript">$(function(){ var gongjv = [ {"name":"康熙111字典","src":"1.html"}, {"name":"康熙222字典","src":"2.html"}, {"name":"康熙333字典","src":"3.html"} ]; var _html=""; for(var i=0;i<gongjv.length;i++){ _html += '<li><a href="'+gongjv[i].src+'" class="zidian"><i class="icon icon-kxzd"></i><p>'+gongjv[i].name+'</p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/cb6835dc7db1" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">前端免费学习笔记(深入)</a>”;</p></a></li>'; } $("#listHTML").html(_html);});</script><ul id="listHTML"></ul>得需要楼上那样的,在js里写
<li><script type="text/javascript">document.write('<a href="'+GongJu[1]+'"><i class="icon icon-kxzd"></i><p>康熙字典</p></a>');</script></li> 第二种方法
<li><a href="" id="aaa"><i class="icon icon-kxzd"></i><p>康熙字典</p></a></li><script type="text/javascript">document.getElementById("aaa").href=GongJu[1];</script>
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号