Home  >  Article  >  Web Front-end  >  关于HTML中JS的调用问题_html/css_WEB-ITnose

关于HTML中JS的调用问题_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:29:581110browse

我在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></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>

    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn