How to use jquery in a SharePoint web part correctly - jquery doesn't always work

I am learning how to use jquery with SharePoint. My example draws a red border inside the web part of the content editor when the link is selected. My code works when the SharePoint page is in edit mode, but not after I left the page and returned without editing. The jquery function for some reason does not work ... I'm missing something simple, but not sure what.

Thanks for any help.

Kevin

My main page of the site is connected to the jquery-1.3.2.min.js file as follows:

 <SharePoint:ScriptLink language="javascript" name="jquery-1.3.2.min.js" 
 Defer="true" runat="server"/>

The code for the content editor web part is as follows:

<style type="text/css">
#box
{
    background: red;
    width: 300px;
    height: 300px;
    display: none;
}
</style>


<script type ="text/javascript">
    $(function() {
        $('a').click(function() {
            $('#box').slideDown(2000);
        });
    })
</script>

<div id="box">
<h1>This is text in the box</h1>
</div>
<a href="#">Display Box</a><br />
+5
source share
4

HTML- ScriptLink , , <script> . , .

, , jQuery SharePoint. ( : - , .)

+3

, , -.

SharePoint, script,

<script language="javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"/>

jQuery ,

<script language="javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>

. </script>, .

!

+7

JavaScript-, , , , , , DOM .

- .

<script language="javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>

<script language="javascript">
function doSomething()
{
  // Your code goes here
}

jQuery.event.add(window, "ready", doSomething);
</script>

SharePoint . http://www.muhimbi.com/blog/2009/07/massage-sharepoint-into-submission.html http://www.muhimbi.com/blog/2009/07/automatically-add-search-as-you-type-to.html

+2

, . . LoadAfterUI = "true"

<SharePoint:ScriptLink ID="SPScriptLink" runat="server" LoadAfterUI="true" Localizable="false" Name="sp.js">
</SharePoint:ScriptLink>
+2

All Articles