繁体   English   中英

如何正确引用js文件,以便外部html可以引用这些文件?

[英]How can I reference js files properly so external html can reference those files?

我是Java语言的新手,所以我将尝试正确地问这个问题。 我正在使用拖放文件上传jquery插件。 当前,每个文件项的html是通过js文件从外部引入的:

   var tpl = $('
     <li class="working">
       <input type="text" value="0" data-width="20" data-height="20"'+ ' data-fgColor="#8dc53e" data-readOnly="1" data-bgColor="#e9eaed" />
       <a href="#" id="username" data-type="text" data-pk="1" data-url="/post" data-title="Description">Click to add description</a>
       <span></span>
       <a href="" download id="download-file">
         <div class="download"></div>
       </a>
     </li>
   ');

我遇到的问题是我试图添加“单击以编辑文本”功能(即Jeditable),因此我可以向每个文件项添加文件说明。 问题是html文件本身中引用了Jeditable的javascript。 并且外部化的html无法引用这些js文件。 如何正确引用这些js文件,以便外部化的html可以正常工作?

事情并没有真正加起来,但这正在做我认为您想要做的事情: https : //jsfiddle.net/wqpddqn9/11/

HTML范例

<div class="pane">
  <form enctype="multipart/form-data" action="vendor/plugins/form/drag-drop-uploader/upload.php" method="post" id="upload">
    <div class="drop-uploader" id="drop"> <span class="drop-text hidden-xs"> Drag &amp; Drop Files <br>
              <small>or</small> </span> <a class="btn btn-primary"> Browse Files </a>
      <input type="file" multiple="" name="upl">
      <ul>
        <!-- The file uploads will be shown here -->
        <li class="working">
          <div>
            <canvas height="20px" width="20"></canvas>
            <input type="text" data-bgcolor="#e9eaed" data-readonly="1" data-fgcolor="#8dc53e" data-height="20" data-width="20" value="0" readonly="readonly" />
          </div>
          <a data-title="Click to Add Description" data-url="/post" data-pk="1" data-type="text" id="description" href="#">Click to Add Description.</a>
          <p>where-is-everyone.png <i>281.86 KB</i></p>
          <span></span>
          <a id="download-file" download="" href="#">
            <div class="download"></div>
          </a>
        </li>
      </ul>
    </div>
    <!-- drop -->
  </form>
</div>

CSS示例

.working div {
  display: inline-block;
  width: 20px;
  height: 20px;
}

.working input[type='text'] {
  width: 2em;
  height: 1em;
  position: absolute;
  vertical-align: middle;
  margin-top: 6px;
  margin-left: -17px;
  border: 0px none;
  background: transparent none repeat scroll 0% 0%;
  font: bold 14px Arial;
  text-align: center;
  color: rgb(141, 197, 62);
  padding: 0px;
}

实例jQuery

var tpl = '<input type="text" value="0" data-width="20" data-height="20" data-fgColor="#8dc53e" data-readOnly="1" data-bgColor="#e9eaed" /> <a href="#" id="username" data-type="text" data-pk="1" data-url="/post" data-title="Description">Save</a><span></span><a href="" download id="download-file"><div class="download"></div></a>';

$(document).ready(function() {
  $("#description").click(function() {
    $(this).next("p").after(tpl);
  });
});

根据加载的方式,您可能需要将JQuery添加到HTML的末尾,或切换到.on()如下所示:

$("#description").on('click', function() {

由于您要向该文件添加说明,因此我在文件名之后将文本框附加到了相同的列表项( li )中。 由于不清楚您遇到什么或想要完成什么,因此不确定是否可以完全解决问题。 随时发表评论,提供更多详细信息或编辑您的帖子。

如果使用的是jQuery,则在动态添加html元素时,您可以考虑使用这种方式绑定onClick事件:

$(".element-class").on("click", function(){
    //Do your thing with $(this) selector to handle the clicked element
});

这样可以很好地处理动态添加的元素。 使用jQuery可能是最简单的解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM