首页 > 解决方案 > 在 React 应用程序中扩展 jQuery(自定义链式 jQuery 函数)

问题描述

在你问我这个问题之前,请阅读:我知道 react 取代了 jQuery 99% 的用例。但是,当用户将鼠标悬停在项目上时,我希望事情发生,而且我不知道 react 可以做到这一点。我也有在 jquery 中工作的代码,我不想重写它。

我的大部分 jQuery 代码似乎都可以正常工作。

在我的 jQuery 代码中,我使用了自定义链式函数。我尝试将下面的代码粘贴到 componentDidMount 中,也直接粘贴在 index.html 的底部,但我总是得到:

jQuery 没有定义

jQuery.fn.extend(
{
 // returns the collection of td cells in the same column as obj_td
tableColumn: function(){
  const $td_this= ancestorTagName("td")
  // both td with right version
  var rows = this.ancestorTagName("table").find('tr');
  console.log("chain tableColumn rows=", rows)
  if (rows === $()) return $() // probably called outside a table
  // index is the number of siblings prior to this one  
  var index = $td_this.prevAll().length;  
  console.log("chain tableColumn index=", index)
  let cols = rows.find(':nth-child(' + (index + 1) + ')')
  cols=cols.filter4tag('td')
  return cols
  },
   ...
  }

目的是使用诸如

let $top_Product = jqObj.tableColumn().find('.version') 

使用.version类在同一列中查找单元格。

我确实有import $ from 'jquery'; 在 App.js 的顶部,我确实将 jquery 组件包含到了 REACT 项目中。

标签: jqueryreactjsfunctionchained

解决方案


推荐阅读