使用一个小的Javascript代码,将clicked-href中的文本添加到Wikipedia页面的右上角。例如,如果我点击Sly and Robbie中的Jamaican
链接,则文本框会附加到右上角的页面中。
注意:要测试此代码,请将其复制并粘贴到控制台,然后单击某些链接。
// Detect click on href
$("a").click(function(event){
// Capture the href from the selected link
var link = this.href;
$.ajax({
type: 'get',
url: link
}).done(function(data){
// Find only the body text (not the titles.. or unnecessary text tips)
var bodyText = $(data).find('#bodyContent #mw-content-text p').text();
// Get the length of the text
var length = bodyText.length;
// Replace the text only if there is text in the clicked link
if(length > 0) {
replaceThumbnail(bodyText);
} else {
alert("No text found!");
}
});
// Prevent the link from being executed
return false;
});
/**
* Append clicked body text to page thumbnail
* @param {string} text Body text
*/
function replaceThumbnail(text) {
$(".infobox .vcard .plainlist").addClass($(".infobox").html(text));
}
我现在的问题是replaceThumbnail
功能。我正在尝试将(正在点击的链接的)正文添加到维基百科页面缩略图顶部的右上角。但是,看起来这个代码,我只是用这个文本替换缩略图。
答案 0 :(得分:0)
该方法的问题在于您的选择器
$(".infobox")
选择整个框并用文本替换其内容。
.html(text)
我在下面修改了你的replaceThumbnail函数:
function replaceThumbnail(text) {
$(".infobox .vcard .plainlist").addClass($(".infobox tbody").prepend(text));
}
这会将您的文字放在信息框的最顶部。