jQuery.html()

Most of the time jQuery.html() is used to just set all the html() in an html element. Very useful for all kinds of stuff. Sometimes though, you want to process and alter the html inside a given element. This can be done by passing a function to html(). Below I put bold tags around the words “data”, “the”, “SVM” and “SVNs”.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<!DOCTYPE html>
<html> 
  <head> 
    <meta charset="utf-8" />
    <title>Html Function</title> 
    <script src="http://code.jquery.com/jquery-1.5.1.min.js"></script>
 
    <script>
      $(function(){
 
        $("p").html(function(i, htm){
          return htm.replace(/\\b(data|the|SVM[s]?)\\b/g, "<b>$1<\/b>");
        });
 
      });
    </script>
    <style>
      b{
        color : red; 
      }
    </style>
  </head> 
 
  <body> 
    <p>
      In computer science, support vector machines (SVMs) are a set of related supervised learning methods that analyze data and recognize patterns, used for classification and regression analysis. The original SVM algorithm was invented by Vladimir Vapnik and the current standard incarnation (soft margin) was proposed by Corinna Cortes and Vladimir Vapnik.[1] The standard SVM takes a set of input data and predicts, for each given input, which of two possible classes the input is a member of, which makes the SVM a non-probabilistic binary linear classifier. Since an SVM is a classifier, then given a set of training examples, each marked as belonging to one of two categories, an SVM training algorithm builds a model that assigns new examples into one category or the other. Intuitively, an SVM model is a representation of the examples as points in space, mapped so that the examples of the separate categories are divided by a clear gap that is as wide as possible. New examples are then mapped into that same space and predicted to belong to a category based on which side of the gap they fall on.
    </p>
    <p>
      More formally, a support vector machine constructs a hyperplane or set of hyperplanes in a high or infinite dimensional space, which can be used for classification, regression, or other tasks. Intuitively, a good separation is achieved by the hyperplane that has the largest distance to the nearest training data points of any class (so-called functional margin), since in general the larger the margin the lower the generalization error of the classifier.
    </p>
  </body> 
</html>

The result looks like this:

This text is from wikipedia’s entry on Support Vector Machines.



 
 
 

Leave a Reply

Spam protection by WP Captcha-Free