首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用jQuery和userscript更改表数据值

使用jQuery和userscript更改表数据值
EN

Stack Overflow用户
提问于 2013-10-24 19:01:33
回答 1查看 438关注 0票数 0

我正在尝试使用jQuery和userscript更改表格数据值,但它不起作用。下面是我的代码:

代码语言:javascript
复制
// ==UserScript==
// @name       My Fancy New Userscript
// @namespace  http://use.i.E.your.homepage/
// @version    0.1
// @description  enter something useful
// @copyright  2012+, You
// ==/UserScript==


// a function that loads jQuery and calls a callback function when jQuery has finished loading
function addJQuery(callback) {
  var script = document.createElement("script");
  script.setAttribute("src", "//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js");
  script.addEventListener('load', function() {
    var script = document.createElement("script");
    script.textContent = "window.jQ=jQuery.noConflict(true);(" + callback.toString() + ")();";
    document.body.appendChild(script);
  }, false);
  document.body.appendChild(script);
}

// the guts of this userscript
function main() {
  // Note, jQ replaces $ to avoid conflicts.
 $('.last').html('111');
}

// load jQuery and execute the main function
addJQuery(main);

我做错了什么?我正在使用Chrome 30.0.1599.69上的Tampermonkey扩展

EN

回答 1

Stack Overflow用户

发布于 2013-10-25 01:45:11

该代码最直接的错误是$('.last').html('111');需要为JQ('.last').html('111');

addJQuery()定义了JQ,而不是main()中的$,这不是addJQuery()的最佳版本。

但是,更大的问题是并不是在坦帕猴(或Greasemonkey)上使用jQuery的聪明方式。尽可能将您的脚本与页面代码隔离。

您的整个脚本应该类似于:

代码语言:javascript
复制
// ==UserScript==
// @name     _YOUR_SCRIPT_NAME
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @version  0.1
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/
$('.last').html ('111');

确保将@include指令与您的目标页面匹配。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19564107

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档