jQuery - function runs fine when fired on div click, but not on page load

后端 未结 5 960
[愿得一人]
[愿得一人] 2021-01-25 17:49

I have the following function that I want to run on page load and re-run when you click div#trigger. When you click div#trigger everything is fine. However on page load the code

5条回答
  •  离开以前
    2021-01-25 18:32

    Try as below

      $(document).ready(function () {
    $('#trigger').click(function(){
        textIndentFunc();
        alert('fire');
     });
    });
    
    function textIndentFunc () {
    
         textString = $('#from-1 :selected').text();
    
         $('#hidden').text(textString);
    
         textWidth = $('#hidden').width();
    
         inputWidth = $('#from-1 select').width();
    
         indentMy = (inputWidth / 2) - (textWidth / 2);
    
        $('#from-1').css('text-indent',indentMy);
    
    }
    textIndentFunc();
    Hai

提交回复
热议问题