gpt4 book ai didi

jquery - 使用 Greasemonkey 将可拖动窗口添加到页面

转载 作者:行者123 更新时间:2023-12-01 03:17:33 25 4
gpt4 key购买 nike

我正在尝试创建一个 Greasemonkey 脚本,为每个网页添加一个可拖动的 div。由于某种原因,div 根本不显示。这可能是什么原因?

// ==UserScript==
// @name Draggable box demo
// @namespace http://use.i.E.your.homepage/
// @version 0.1
// @description enter something useful
// @match *://www.*
// @copyright 2012+, You
// @require http://code.jquery.com/jquery-latest.js http://code.jquery.com/ui/1.9.2/jquery-ui.js
// ==/UserScript==
//alert("Hi!");

$(document).ready(function() {
$(document).append("<div id='dragZone'><div class='draggable'>Drag here!<input type = 'text'></input></div>");
$('#dragZone').css('position', 'absolute');
var a = 3;
$('.draggable').draggable({
start: function(event, ui) { $(this).css("z-index", a++); }
});
$('#dragZone div').mousedown(function() {
$(this).addClass('top').removeClass('bottom');
$(this).siblings().removeClass('top').addClass('bottom');
$(this).css("z-index", a++);
});
});

最佳答案

第一次就完全错误 - 问题在于使用 $(document).append。您不能直接附加到文档,只能附加到节点。

所以要么

$(document.body).append()

$('body').append()

Here's the fiddle for proof.

可能是缺少@require,也许你的greasemonkey已经过时了?

// ==UserScript==
// @name My Fancy New Userscript
// @namespace http://use.i.E.your.homepage/
// @version 0.1
// @description enter something useful
// @match http://*/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js
// @copyright 2012+, You
// ==/UserScript==
jQuery(function($){
var _highest = 0;

$("div").each(function() {
var _current = parseInt($(this).css("zIndex"), 10);
if(_current > _highest) {
_highest = _current + 1;
}
});
$('body').append('<div style="position:absolute;top:50px;z-index:'+_highest+';left:100px;background:#ecebeb;border:1px solid #333;border-radius:5px;height:50px;width:300px;"> Hello, This is an addon div from Greasemonkey. </div>');
});

样板模板。 OOB 应该可以正常工作。

关于jquery - 使用 Greasemonkey 将可拖动窗口添加到页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13946047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com