【问题标题】:Issue with document.ready function in jQueryjQuery 中 document.ready 函数的问题
【发布时间】:2014-07-03 13:20:01
【问题描述】:

我在使用 document.ready jQuery 函数时遇到了一个问题。

在加载时 document.ready 功能工作正常。当我单击按钮或 href 链接时,我想重新连接 document.ready 函数,其中我在 JavaScript 文件中有一组代码。

这是实际情况。请在下面找到示例 JS 代码:

$(document).ready(function() {
      var title = "This is your title";    
      var shortText = jQuery.trim(title).substring(0, 10).split(" ").slice(0, -1).join(" ") + "...";    
alert(shortText );
});

单击提交按钮后,我将在下表行中添加输入字段数据。其中描述文本也添加在表格行列之一中。如果描述字段超过 100 个字符,我将从外部 .JS 文件推送上述 JavaScript 代码。如何在不刷新页面的情况下刷新 Java 脚本功能?有人知道吗?

请分享您的意见。

【问题讨论】:

    标签: truncate document-ready


    【解决方案1】:

    创建一个函数,可以在 document.ready 中调用,也可以在其他任何地方调用,例如按钮点击事件:

    function myfunc(){
        var title = "This is your title";    
        var shortText = jQuery.trim(title).substring(0, 10).split(" ").slice(0, -1).join(" ") + "...";    
        alert(shortText );
    }
    
    $(document).ready(function() {
          //call  when document is ready
          myfunc();
          //call again when button is clicked
          $('#button').click(myfunc());
    });
    

    【讨论】:

      猜你喜欢
      • 2011-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多