jQuery - 在div点击时触发功能运行正常,但不能在页面加载时运行 [英] jQuery - function runs fine when fired on div click, but not on page load
问题描述
我有以下功能,我想在页面加载时运行,并在点击div#trigger时重新运行。当你点击div#触发一切都很好。然而,在页面加载代码似乎有时工作,但有时缩进错误的数量,警报(我只是为了调试目的放在那里)永远不会触发。谢谢
function textIndentFunc(){
textString = $('#from-1:selected' )。文本();
$('#hidden')。text(textString);
textWidth = $('#hidden')。width();
inputWidth = $('#from-1 select')。width();
indentMy =(inputWidth / 2) - (textWidth / 2);
$('#from-1')。css('text-indent',indentMy);
$ b $('#trigger')。click(function(){
textIndentFunc();
alert('fire');
});
textIndentFunc();
更新 - 我应该提到这个代码在document.ready上运行。
UPDATE - 将代码移到window.load不会改变任何内容。
你有另一个函数也可以在页面加载时运行吗?因为如果是这种情况,那么这两个函数可能会有冲突......
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 appears to sometimes work but sometimes indent the the wrong amount, and the alert (which i just put in there for debugging purposes) never fires. Thanks
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);
}
$('#trigger').click(function(){
textIndentFunc();
alert('fire');
});
textIndentFunc();
UPDATE - I should have mentioned this code runs on document.ready.
UPDATE - moving the code to window.load doesn't change anything.
Do you have another function that also runs on page load? Because if this is the case maybe the 2 functions have a conflict...
这篇关于jQuery - 在div点击时触发功能运行正常,但不能在页面加载时运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!