jQuery 在控制台中有效,但在 .js 文件中无效
jQuery works in console but not in .js file
$('ul#test a').click(function(){
$('this').closest('parent').find('textarea').val(this.getAttribute('href'));
});
在 Google Chrome
片段中有效,但未加载 Tampermonkey
。
我认为它试图在找到目标之前设置一个值
(编辑)
整个脚本:
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match www.reddit.com/r/some-subreddit
// @grant none
// ==/UserScript==
(function() {
'use strict';
$( "textarea" ).one("click",(function() {
$(".usertext-edit").prepend ( `
<div id="gmSomeID"><ul id="gmSomeeID"><a href="#stickers1"</a> <a href="#stickers2"</a> <a href="#stickers3"</a> <a href="#stickers4"</a> <a href="#stickers5"</a ><a href="#stickers6"</a> <a href="#stickers7"</a> <a href="#stickers1"</a> <a href="#stickers2"</a> <a href="#stickers3"</a> <a href="#stickers4"</a> <a href="#stickers5"</a> <a href="#stickers6"</a>
</ul></div>
` );
$('ul#gmSomeeID').css({
'overflow-y':'scroll',
'display':'block',
'overflow-wrap' : 'normal',
'height':'100px',
'background-color':'white'
});
$('ul#gmSomeeID a').click(function(){
$("this").closest("div.usertext-edit")
.find("textarea")
.val($("this")
.closest("div.usertext-edit")
.find("textarea")
.val() +' '+ '[](' + this.getAttribute('href') + ') ');
});
}));
})();
它在文本区域上方显示了一些图片,我试图让它在单击它们时将它们插入到所述文本区域中
将您的代码放在 ready
函数中,这样事件就会在您的选择器中找到该元素,因为它将在 DOM 完全加载后立即执行:
$(function(){
//Your code here
})
也试试事件委托:
$(function(){
$('body').on('click', '#test a', function(){
$(this).closest('parent').find('textarea').val( $(this).attr('href') );
});
});
注意 1: 代码在控制台中工作,因为 DOM 已完全加载。
注意 2: $('this')
应该是 $(this)
而不是 $(this).attr('href')
而不是 this.getAttribute('href')
。
希望这对您有所帮助。
当您在控制台中尝试代码时,DOM 已经完成构建。如果您有显示的代码,在文件中的所有 HTML 之前,它将尝试在元素被解析到内存之前找到元素。
将您的代码放在 document.ready 处理程序中:
$(function(){
$('ul#test a').click(function(){
$('this').closest('parent').find('textarea').val(this.getAttribute('href'));
});
});
$('ul#test a').click(function(){
$('this').closest('parent').find('textarea').val(this.getAttribute('href'));
});
在 Google Chrome
片段中有效,但未加载 Tampermonkey
。
我认为它试图在找到目标之前设置一个值
(编辑) 整个脚本:
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match www.reddit.com/r/some-subreddit
// @grant none
// ==/UserScript==
(function() {
'use strict';
$( "textarea" ).one("click",(function() {
$(".usertext-edit").prepend ( `
<div id="gmSomeID"><ul id="gmSomeeID"><a href="#stickers1"</a> <a href="#stickers2"</a> <a href="#stickers3"</a> <a href="#stickers4"</a> <a href="#stickers5"</a ><a href="#stickers6"</a> <a href="#stickers7"</a> <a href="#stickers1"</a> <a href="#stickers2"</a> <a href="#stickers3"</a> <a href="#stickers4"</a> <a href="#stickers5"</a> <a href="#stickers6"</a>
</ul></div>
` );
$('ul#gmSomeeID').css({
'overflow-y':'scroll',
'display':'block',
'overflow-wrap' : 'normal',
'height':'100px',
'background-color':'white'
});
$('ul#gmSomeeID a').click(function(){
$("this").closest("div.usertext-edit")
.find("textarea")
.val($("this")
.closest("div.usertext-edit")
.find("textarea")
.val() +' '+ '[](' + this.getAttribute('href') + ') ');
});
}));
})();
它在文本区域上方显示了一些图片,我试图让它在单击它们时将它们插入到所述文本区域中
将您的代码放在 ready
函数中,这样事件就会在您的选择器中找到该元素,因为它将在 DOM 完全加载后立即执行:
$(function(){
//Your code here
})
也试试事件委托:
$(function(){
$('body').on('click', '#test a', function(){
$(this).closest('parent').find('textarea').val( $(this).attr('href') );
});
});
注意 1: 代码在控制台中工作,因为 DOM 已完全加载。
注意 2: $('this')
应该是 $(this)
而不是 $(this).attr('href')
而不是 this.getAttribute('href')
。
希望这对您有所帮助。
当您在控制台中尝试代码时,DOM 已经完成构建。如果您有显示的代码,在文件中的所有 HTML 之前,它将尝试在元素被解析到内存之前找到元素。
将您的代码放在 document.ready 处理程序中:
$(function(){
$('ul#test a').click(function(){
$('this').closest('parent').find('textarea').val(this.getAttribute('href'));
});
});