jquery 添加/删除tag
实时添加删除tag
![jquery 添加/删除tag jquery 添加/删除tag](https://i-blog.csdnimg.cn/blog_migrate/da7e2b4bd7c20b03ce5aff46aefcae2d.jpeg)
XML/HTML Code
- <div class="main">
- <div class="tagtacular_textlike" id="tagtacular_1"></div>
- <div class="codebox">
- </div>
- <h3>Example 2: Keep it Simple</h3>
- <p>
- Using the tagtacular_basic style and the default configuration. Edit and view modes, starting in edit, with Add and Switch Mode buttons. Enter or comma add a new tag, as does the Add button.
- </p>
- <div class="tagtacular_basic" id="tagtacular_2"></div>
- <div class="codebox">
- </div>
- <h3>Example 3: Limit to Existing Tags</h3>
- <p>
- Similar to the last example, but always in edit mode (no mode switch button). No add button or add delimiters, so we restrict the user to adding from a predefined list of tags.
- </p>
- <div class="tagtacular_basic red" id="tagtacular_3"></div>
- <div class="codebox">
- </div>
- <h3>Example 4: Custom Validator and Commit Functions</h3>
- <p>
- Using the tagtacular_basic.blue style. Here we change validation behavior and run code to commit changes to the backend (simulated by sending a success message two seconds later). This instance is also configured to start in view mode. This is also a good example of how you can call any callback defined in your settings object from any other callback that gets a settings object.
- </p>
- <div class="tagtacular_basic blue" id="tagtacular_4"></div>
- <div class="codebox">
- </div>
- <h3>Example 5: Custom Tags</h3>
- <p>
- Using getTagHtml setting to define custom tag html. Here we make each tag a link to a Wikipedia search. You could apply this general method to make each tag link to a search that returned all entities with that tag in your web app. We're also applying a filter on the tag names themselves using formatTagName and configFormatTagNamesOnInit. The first letter of each word is capitalized and the rest is converted to lower case.
- </p>
- <div class="tagtacular_basic yellow" id="tagtacular_5"></div>
- <div class="codebox">
- </div>
- </div>
- <h3>Example 6: Edit Bar On Top</h3>
- <p>
- Moving the edit tray to before the tag tray with the configEditTrayFirst setting. More layout customization would have been possible with the getLayoutHtml callback setting. Also, we're giving the buttons some custom text. We're also disabling add tag on switch mode and setting some placeholder text.
- </p>
- <div class="tagtacular_basic orange" id="tagtacular_6"></div>
- <div class="codebox">
- </div>
- <h3>Example 7: Limit to Existing Tags, Use a Select Box</h3>
- <p>
- Let's use a select box! Also, disable edit/view mode switching by setting configShowSwitchButton to false.
- </p>
- <div class="tagtacular_basic" id="tagtacular_7"></div>
- <div class="codebox">
- </div>
- </div>
- <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>
- <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
- <!-- <script type="text/javascript" src="js/tagtacular.min.js"></script> -->
- <script type="text/javascript" src="js/tagtacular.js?donotcache=20130703"></script>
- <script type="text/javascript">
- var tags1 = $('#tagtacular_1').tagtacular({
- entityId: 101,
- entityTags: ['Boston', 'Chicago', 'New York City', 'Atlanta'],
- systemTags: ['Austin', 'Dallas', 'Raleigh', 'Richmond', 'Augusta', 'Portland', 'Durham', 'Memphis', 'Nashville', 'Seattle', 'Toronto', 'Boston'],
- configTagSeparator: ', ',
- configShowAddButton: false,
- configShowSwitchButton: false,
- configDeleteLastOnEmptyKeys: [8],
- configSortTags: false
- });
- var tags2 = $('#tagtacular_2').tagtacular({
- entityId: 102,
- entityTags: ['Boston', 'Chicago', 'New York City', 'Atlanta'],
- systemTags: ['Austin', 'Dallas', 'Raleigh', 'Richmond', 'Augusta', 'Portland', 'Durham', 'Memphis', 'Nashville', 'Seattle', 'Toronto'],
- });
- var tags3 = $('#tagtacular_3').tagtacular({
- entityId: 103,
- entityTags: ['Alpha', 'Beta', 'Epsilon'],
- systemTags: ['Alpha', 'Beta', 'Gamma', 'Delta', 'Epsilon', 'Zeta', 'Eta', 'Theta', 'Iota', 'Kappa',
- 'Lambda', 'Mu', 'Nu', 'Xi', 'Omicron', 'Pi', 'Rho', 'Sigma', 'Tau', 'Upsilon', 'Phi',
- 'Chi', 'Psi', 'Omega'],
- configShowAddButton: false,
- configDelimiters: []
- });
- var tags4 = $('#tagtacular_4').tagtacular({
- entityId: 104,
- entityTags: ['Boston', 'Chicago', 'New York City', 'Atlanta'],
- systemTags: ['Austin', 'Dallas', 'Raleigh', 'Richmond', 'Augusta', 'Portland', 'Durham', 'Memphis', 'Nashville', 'Seattle', 'Toronto'],
- configMinimumTagLength: 2,
- configMaximumTagLength: 24,
- validationPattern: /^[0-9A-Za-z\.]+$/,
- messageTagNameInvalid: 'illegal characters: tag names can only include letters, numbers, and periods',
- commitAddTag: function(tag, entityId, settings) {
- setTimeout(function() {
- settings.flashSuccess('saved add of ' + tag + ' to entity #' + entityId);
- }, 2000);
- },
- commitRemoveTag: function(tag, entityId, settings) {
- setTimeout(function() {
- settings.flashSuccess('saved remove of ' + tag + ' from entity #' + entityId);
- }, 2000);
- },
- mode: 'view'
- });
- var tags5 = $('#tagtacular_5').tagtacular({
- entityId: 105,
- entityTags: ['boston', 'chicago', 'new york city', 'atlanta'],
- systemTags: ['austin', 'dallas', 'raleigh', 'richmond', 'augusta', 'portland', 'durham', 'memphis', 'nashville', 'seattle', 'toronto'],
- getTagHtml: function(tag, mode, settings) {
- var encodedTag = tag.replace(' ', '+');
- encodedTagencodedTag = encodedTag.replace('-', '+');
- encodedTagencodedTag = encodedTag.replace('_', '+');
- var url = 'http://en.wikipedia.org/wiki/Special:Search?search=' + encodedTag + '&go=Go';
- if (mode=='edit') {
- return '<span class="tagtacular_tag"><a href="'+url+'" target="_blank" ><span class="tagtacular_value">'+tag+'</span></a> <a class="tagtacular_delete" href="#">'+settings.configDeleteSymbol+'</a><span class="tagtacular_delim">'+settings.configTagSeparator+'</span></span>';
- } else if (mode=='view') {
- return '<span class="tagtacular_tag"><a href="'+url+'" target="_blank" ><span class="tagtacular_value">'+tag+'</span></a><span class="tagtacular_delim">'+settings.configTagSeparator+'</span></span>';
- }
- },
- formatTagName: function(tag) {
- // capitalize first letter of each word and make the rest lower case
- tagtag = tag.toLowerCase();
- return tag.replace(/(^([a-zA-Z\p{M}]))|([ -_][a-zA-Z\p{M}])/g,
- function($1){
- return $1.toUpperCase();
- });
- },
- configFormatTagNamesOnInit: true
- });
- var tags6 = $('#tagtacular_6').tagtacular({
- entityId: 106,
- entityTags: ['Boston', 'Chicago', 'New York City', 'Atlanta'],
- systemTags: ['Austin', 'Dallas', 'Raleigh', 'Richmond', 'Augusta', 'Portland', 'Durham', 'Memphis', 'Nashville', 'Seattle', 'Toronto'],
- configEditTrayFirst: true,
- configAddButtonText: 'Boom! Add it.',
- configSwitchButtonTextInEdit: 'No more change!',
- configSwitchButtonTextInView: 'It\'s time for a change!',
- configPlaceholderText: 'Wanna add a tag?',
- configAddOnSwitch: false
- })
- var tags7 = $('#tagtacular_7').tagtacular({
- entityId: 107,
- entityTags: ['Alpha', 'Beta', 'Epsilon'],
- systemTags: ['Alpha', 'Beta', 'Gamma', 'Delta', 'Epsilon', 'Zeta', 'Eta', 'Theta', 'Iota', 'Kappa',
- 'Lambda', 'Mu', 'Nu', 'Xi', 'Omicron', 'Pi', 'Rho', 'Sigma', 'Tau', 'Upsilon', 'Phi',
- 'Chi', 'Psi', 'Omega'],
- configEditTrayFirst: true,
- configSelectBox: true,
- configShowSwitchButton: false
- })
- // Here's how to use the hooks to manipulate an instance of tagtacular
- // tags1.addTag('Detroit');
- // tags1.removeTag('Chicago');
- // console.log(tags1.getEntityTags());
- // console.log(tags1.getSystemTags());
- // console.log(tags1.getRemainingTags());
- // console.log(tags1.getState());
- // console.log(tags1.getEntityId());
- </script>