我的jquery方法不起作用

时间:2016-04-13 10:44:40

标签: javascript jquery highlighting

我真的很困惑。我有一个javascript代码,发布到webmethod并获取字符串。并且有一个h1标记。我想在网站上突出显示返回的单词(' s" abc" here)。所以我的aspx代码如下。我的jquery方法(突出显示)不起作用。任何人都可以帮忙吗?

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" 
AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication4._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"></asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

<!doctype html>
<html lang="en">
<head>
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script src="http://www.google.com/jsapi" type="text/javascript"></script>
    <script type="text/javascript">
      google.load('visualization', '1.1', { packages: ['corechart', 'controls'] });
    </script>

    <script src="Scripts/jquery-1.11.3.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery-ui.min.js" type="text/javascript"></script>
</head>
<body>

<h1> abc sadaf dasfafa dsada abc afasgfa abc </h1>

<script>
    var exp;
    jQuery.fn.highlight = function (str, className) {
      var regex = new RegExp(str, "gi");
      return this.each(function () {
          $(this).contents().filter(function () {
              return this.nodeType == 3 && regex.test(this.nodeValue);
          }).replaceWith(function () {
              return (this.nodeValue || "").replace(regex, function (match) {
                return "<span class=\"" + className + "\">" + match + "</span>";
              });
          });
      });
    };

    $.ajax({
      type: 'POST',
      url: 'http://localhost:61216/Default.aspx/MyFunction',
      contentType: 'application/json; charset=utf-8',
      dataType: 'json',
      success: function (result) {
        exp = result.d;
      },
      error: function () {
        alert('Something occured');
      }
    });

    $(document).on('click', 'h1', function(){
      $('h1').highlight(exp, "highlight");
      alert("hi");
    });
</script>

</body>
</html>
</asp:Content>

1 个答案:

答案 0 :(得分:1)

您的jquery功能正常,如下所示:

&#13;
&#13;
 var exp = 'abc';
 jQuery.fn.highlight = function(str, className) {
   var regex = new RegExp(str, "gi");
   return this.each(function() {
     $(this).contents().filter(function() {
       return this.nodeType == 3 && regex.test(this.nodeValue);
     }).replaceWith(function() {
       return (this.nodeValue || "").replace(regex, function(match) {
         return "<span class=" + className + ">" + match + "</span>";
       });
     });
   });
 };

 $(document).on('click', 'h1', function() {
   $('h1').highlight(exp, "highlight");
 });
&#13;
.highlight {
  color: red;
  background-color: yellow;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<h1> abc sadaf dasfafa dsada abc afasgfa abc </h1>
&#13;
&#13;
&#13;

问题是您的ajax请求的success回调。 只有在用户点击highlight元素时才会调用h1函数。因此,当您的ajax请求结束时,您需要触发该功能以突出显示新的exp。尝试使用下面的代码替换成功回调,然后重试:

success: function (result) {
  exp = result.d;
  $('h1').highlight(exp, "highlight");
}