var myObjectLiteral = {
myBehavior1 : function () {
/* - */
},
myBehavior2 : function () {
/* */
}
};
* This source code was highlighted with Source Code Highlighter .
$( document ).ready( function () {
$( '#myFeature li' )
.append( '<div/>' )
.each( function () {
$( this ).find( 'div' )
.load( 'foo.php?item=' + $( this ).attr( 'id' ));
})
.click( function () {
$( this ).find( 'div' ).show();
$( this ).siblings().find( 'div' ).hide();
});
});
* This source code was highlighted with Source Code Highlighter .
var myFeature = {
config : {
wrapper : '#myFeature' ,
container : 'div' ,
urlBase : 'foo.php?item='
},
init : function (config) {
$.extend(myFeature.config, config);
$(myFeature.config.wrapper).find( 'li' ).
each( function () {
myFeature.getContent($( this ));
}).
click( function () {
myFeature.showContent($( this ));
});
},
buildUrl : function ($li) {
return myFeature.config.urlBase + $li.attr( 'id' );
},
getContent : function ($li) {
$li.append(myFeature.config.container);
var url = myFeature.buildUrl($li);
$li.find(myFeature.config.container).load(url);
},
showContent : function ($li) {
$li.find( 'div' ).show();
myFeature.hideContent($li.siblings());
},
hideContent : function ($elements) {
$elements.find( 'div' ).hide();
}
};
$( document ).ready( function () { myFeature.init(); });
* This source code was highlighted with Source Code Highlighter .
< div id ="myFeature" >
< ul class ="sections" >
< li >
< h2 >< a href ="/section/1" > Section 1 </ a ></ h2 >
< ul >
< li >
< h3 >< a href ="/section/1/content/1" > Section 1 Title 1 </ a ></ h3 >
< p > The excerpt content for Content Item 1 </ p >
</ li >
< li >
< h3 >< a href ="/section/1/content/2" > Section 1 Title 2 </ a ></ h3 >
< p > The excerpt content for Content Item 2 </ p >
</ li >
< li >
< h3 >< a href ="/section/1/content/3" > Section 1 Title 3 </ a ></ h3 >
< p > The excerpt content for Content Item 3 </ p >
</ li >
</ ul >
</ li >
< li >
< h2 >< a href ="/section/2" > Section 2 </ a ></ h2 >
< ul >
< li >
< h3 >< a href ="/section/2/content/1" > Section 2 Title 1 </ a ></ h3 >
< p > The excerpt content for Content Item 1 </ p >
</ li >
< li >
< h3 >< a href ="/section/2/content/2" > Section 2 Title 2 </ a ></ h3 >
< p > The excerpt content for Content Item 2 </ p >
</ li >
< li >
< h3 >< a href ="/section/2/content/3" > Section 2 Title 3 </ a ></ h3 >
< p > The excerpt content for Content Item 3 </ p >
</ li >
</ ul >
</ li >
< li >
< h2 >< a href ="/section/3" > Section 3 </ a ></ h2 >
< ul >
< li >
< h3 >< a href ="/section/3/content/1" > Section 3 Title 1 </ a ></ h3 >
< p > The excerpt content for Content Item 1 </ p >
</ li >
< li >
< h3 >< a href ="/section/3/content/2" > Section 3 Title 2 </ a ></ h3 >
< p > The excerpt content for Content Item 2 </ p >
</ li >
< li >
< h3 >< a href ="/section/3/content/3" > Section 3 Title 3 </ a ></ h3 >
< p > The excerpt content for Content Item 3 </ p >
</ li >
</ ul >
</ li >
</ ul >
</ div >
* This source code was highlighted with Source Code Highlighter .
var myFeature = {
'config' : { },
'init' : function () { },
'buildSectionNav' : function () { },
'buildItemNav' : function () { },
'showSection' : function () { },
'showContentItem' : function () { }
};
* This source code was highlighted with Source Code Highlighter .
myFeature.config
, which stores in one place the default values. We will add the ability to override the default values ​​when we define the myFeature.init ()
method.myFeature.config
object and method myFeature.init ()
:'config' : {
// #myFeature
'container' : $( '#myFeature' )
},
'init' : function (config) {
// init()
if (config && typeof (config) == 'object' ) {
$.extend(myFeature.config, config);
}
// / DOM
//
myFeature.$container = myFeature.config.container;
myFeature.$sections = myFeature.$container.
//
find( 'ul.sections > li' );
myFeature.$section_nav = $( '<p/>' )
.attr( 'id' , 'section_nav' )
.prependTo(myFeature.$container);
myFeature.$item_nav = $( '<p/>' )
.attr( 'id' , 'item_nav' )
.insertAfter(myFeature.$section_nav);
myFeature.$content = $( '<p/>' )
.attr( 'id' , 'content' )
. insertAfter(myFeature.$item_nav);
//
// ""
myFeature.buildSectionNav(myFeature.$sections);
myFeature.$section_nav.find( 'li:first' ).click();
// HTML
myFeature.$container.find( 'ul.sections' ).hide();
// ( )
myFeature.initialized = true ;
}
* This source code was highlighted with Source Code Highlighter .
myFeature.buildSectionNav ()
method:'buildSectionNav' : function ($sections) {
//
$sections.each( function () {
var $section = $( this );
//
$( '<li/>' )
// h2
//
.text($section.find( 'h2:first' ).text())
//
.appendTo(myFeature.$section_nav)
// data()
.data( 'section' , $section)
// click
.click(myFeature.showSection);
});
* This source code was highlighted with Source Code Highlighter .
myFeature.buildItemNav()
:'buildItemNav' : function ($items) {
$items.each( function () {
var $item = $( this );
//
$( '<li>' )
// h3
//
.text($item.find( 'h3:first' ).text())
// add the list item to the item navigation
.appendTo(myFeature.$item.nav)
// data()
.data( 'item' , $item)
// click
.click(myFeature.showContentItem);
});
* This source code was highlighted with Source Code Highlighter .
'showSection' : function () {
var $li = $( this );
//
myFeature.$item_nav.empty();
myFeature.$content.empty();
// jQuery
// data() buildSectionNav
var $section = $li.data( 'section' );
//
$li.addClass( 'current' )
.siblings().removeClass( 'current' );
//
var $items = $section.find( 'ul li' );
//
myFeature.buildItemNav($items);
// ""
myFeature.$item_nav.find( 'li:first' ).click();
},
'showContentItem' : function () {
var $li = $( this );
//
$li.addClass( 'current' )
.siblings().removeClass( 'current' );
// jQuery
// data() buildSectionNav
var $item = $li.data( 'item' );
//
myFeature.$content.html($item.html());
}
* This source code was highlighted with Source Code Highlighter .
myFeature.init()
:$( document ).ready(myFeature.init);
* This source code was highlighted with Source Code Highlighter .
var myFeature = {
'config' : {
'container' : $( '#myFeature' ),
//
// URL
'getItemURL' : function ($item) {
return $item.find( 'a:first' ).attr( 'href' );
}
},
'init' : function (config) {
//
},
'buildSectionNav' : function ($sections) {
//
},
'buildItemNav' : function ($items) {
//
},
'showSection' : function () {
//
},
'showContentItem' : function () {
var $li = $( this );
$li.addClass( 'current' ).
siblings().removeClass( 'current' );
var $item = $li.data( 'item' );
var url = myFeature.config.getItemURL($item);
// myFeature.$content.html($item.html());
myFeature.$content.load(url);
}
};
* This source code was highlighted with Source Code Highlighter .
var myFeature = {
'config' : {
'container' : $( '#myFeature' ),
'itemNavSelector' : 'h3' ,
'itemNavProcessor' : function ($selection) {
return 'Preview of ' +
$selection.eq(0).text();
}
},
'init' : function (config) {
//
},
'buildSectionNav' : function ($sections) {
//
},
'buildItemNav' : function ($items) {
$items.each( function () {
var $item = $( this );
var myText = myFeature.config.itemNavProcessor(
$item.find(myFeature.config.itemNavSelector)
);
$( '<li/>' )
.text(myText)
.appendTo(myFeature.$item_nav)
.data( 'item' , $item)
.click(myFeature.showContentItem);
});
},
'showSection' : function () {
//
},
'showContentItem' : function () {
//
}
};
* This source code was highlighted with Source Code Highlighter .
myFeature.init ()
$( document ).ready( function () {
myFeature.init({ 'itemNavSelector' : 'h2' });
});
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/111290/
All Articles