简体   繁体   English

jQuery从外部文件

[英]jquery from external file

I am a newbie to Jquery as will be evident. 很明显,我是Jquery的新手。 I have 2 scripts in an external file named scrip.js and it is included with a simple 我在名为scrip.js的外部文件中有2个脚本,它包含在一个简单的文件中

<script src="/media/javascripts/scrips.js"></script>

the two scripts in scrips.js are scrips.js中的两个脚本是

function textCounter(field,cntfield,maxlimit) {
 if (field.value.length > maxlimit) // if too long...trim it!
 field.value = field.value.substring(0, maxlimit);
 // otherwise, update 'characters left' counter
 else
 cntfield.value = maxlimit - field.value.length;
 }

$(function() {
$("button").click(function(){
$("p").css("color","black");
  });
});

The first script works fine. 第一个脚本工作正常。 the second does not. 第二个没有。 The html for the second scrip looks like this, very simple: 第二个脚本的html看起来很简单:

<span><? echo $row->date, nbs(10), $row->author, nbs(20), anchor("http://twitter.com   /home?status=$twittermsg", 'ReTweet', $tweet), nbs(15), "<button>Black Font</button>"; ?></span> 

The button Black font should be selected by the second script in the external file but it doesnt work. 黑色字体按钮应该由外部文件中的第二个脚本选择,但是它不起作用。 Is there something else to be done to a jquery scrip in an external file to make it work? 还要对外部文件中的jquery脚本进行其他处理才能使其正常工作? Is there anything I should be doing in the html to get it to work? 我需要在html中做些什么才能使其正常工作? (the html is Codeigniter btw) (HTML是Codeigniter btw)

I have read several questions on here about this but they appear very confusing 我在这里已经阅读了几个有关此问题,但它们看起来非常令人困惑

Thank you 谢谢

Did you make sure to include the jquery.js file before you included your script like below: 在包含如下脚本之前,是否确定要包含jquery.js文件:

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="/media/javascripts/scrips.js"></script>

in your given link, I don't see this somewhere 在您给定的链接中,我在某处看不到它

$("button").click(function(){...})

and you have two link scripts for jQuery, one at the header and one at the bottom part. 并且您有两个jQuery链接脚本,一个在标题中,一个在底部。 Just one will do, remove the bottom one. 只需做一个,删除最下面的一个。

See the error console for js error. 有关JS错误,请参见错误控制台。 I did not find any problem in your script. 我在您的脚本中没有发现任何问题。 You can use an alert in button click function to debug your script. 您可以使用单击按钮中的警报功能来调试脚本。 you can use button and p id/class in order to avoid conflict. 您可以使用button和p id / class以避免冲突。

Looks to me like you're missing an ending brace for the function... 在我看来,您缺少该功能的结尾括号...

The last brace that I see ends the if statement. 我看到的最后一个花括号是if语句的结尾。 I believe you need another one to end the function as well: 我相信您还需要另一个来结束该功能:

function textCounter(field,cntfield,maxlimit) {
   if (field.value.length > maxlimit) // if too long...trim it!
       field.value = field.value.substring(0, maxlimit);
       // otherwise, update 'characters left' counter
   else
       cntfield.value = maxlimit - field.value.length;
   }
}

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

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