jquery - 在 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 项目中。
解决方案
推荐阅读
- python - Python - 如何从对象转换为浮点数
- swift - 如何释放 RealityKit ARView()?
- laravel - 在数据库中存储记录后发送 gmail 错误
- c++ - 对象如何存储在数组中?
- c++ - 用于 XSLT 处理以进行 XML 转换的 C++ 库
- python - pandas 中如何打印 col1、col2 和它们的区别?
- deep-learning - 从分类损失修改为回归损失
- bash - 如何打印多个文件的第一行?
- xml - 为什么在 Node-Express 应用程序中未定义 JSON 对象
- c# - Blazor 客户端(Web 程序集)AuthenticationState 仅在页面重新加载后更新