【问题标题】:My jquery method doesn't work我的 jquery 方法不起作用
【发布时间】:2016-08-04 09:47:08
【问题描述】:

我真的很困惑。我有一个发布到 webmethod 并获取字符串的 javascript 代码。还有一个h1 标签。我想突出显示网站上的返回词(这里是“abc”)。所以我的 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>

【问题讨论】:

  • 你能把你的jquery脚本包装在jQuery(document).ready(function(){ /*jquery code */ });
  • 您的高亮代码$('h1').highlight(exp, "highlight"); 应该在Ajax 的成功处理程序中,就在exp = result.d; 行的下方。请尝试并告诉我。
  • 为什么要使用多个版本的jQuery?
  • 我已经按照你们说的做了,但是不起作用:/而且没有控制台错误
  • 顺便说一句,我有另一个项目,当我尝试这个时,它正在那里工作。这就是我在这里添加这些脚本的原因

标签: javascript jquery highlighting


【解决方案1】:

您的 jquery 函数正在运行,如下所示:

 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");
 });
.highlight {
  color: red;
  background-color: yellow;
}
<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>

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

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

【讨论】:

    猜你喜欢
    • 2015-03-04
    • 2015-03-03
    • 2015-11-23
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多