简体   繁体   English

我无法获取JavaScript来更改页面上的文本

[英]I can't get my javascript to change text on a page

Edit2 : A friend informed me that I forgot to mention that this script would load through Greasemonkey Edit2:一位朋友告诉我,我忘了提到此脚本将通过Greasemonkey加载

I'm fairly new to Javascript, and I'm trying to self teach it. 我对Java相当陌生,并且正在尝试自学它。

I've been trying to get text on a page to change using this line of code: 我一直在尝试使用以下代码行来更改页面上的文本:

document.getElementById("gamepage_header").innerHTML = '<h1>Test</h1>'

Instead of doing what I want, it seems to just sit there and do nothing. 似乎只是坐在那里什么都不做,而不是干我想做的事情。 Yes, I've looked in many places to something to work. 是的,我在很多地方都在寻找一些可行的方法。

Thanks 谢谢

Edit: This is the page I'm trying to edit. 编辑:这是我要编辑的页面。 http://www.kongregate.com/games/kChamp/shellshock-live My real idea is to fetch a link within the HTML, then set the gamepage_header as the link so I can simply rightclick and download the .swf. http://www.kongregate.com/games/kChamp/shellshock-live我真正的想法是获取HTML中的链接,然后将gamepage_header设置为链接,这样我就可以右键单击并下载.swf。

As per request, here is my whole "code", if you can call it that. 根据要求,如果可以调用的话,这是我的整个“代码”。

// ==UserScript==
// @name         SWFLinkage for KH
// @namespace   tag://kongregate
// @description SWF Link getter
// @version     1
// @grant           none
// @author          SimpleAOB
// @date            12.7.2012
// ==/UserScript==
document.onload = function(){
   document.getElementById("gamepage_header").innerHTML = '<h1>Test</h1>' 
};

Matt Ball's comment revealed that you need to wait until the page loads. Matt Ball的评论表明,您需要等待页面加载。

The code may be running before gamepage_header is loaded. 该代码可能在gamepage_header加载之前正在运行。 To resolve this, use: 要解决此问题,请使用:

window.onload = function(){
    // Code goes here.
};

This waits until the page loads, then runs the code. 等待直到页面加载,然后运行代码。 Don't worry about the syntax; 不用担心语法。 you will learn soon enough as you gain more skill in JS. 随着您掌握JS的更多技能,您将很快学到东西。

you must ensure the page has loaded before running your script as you are trying to access the DOM before it has completely loaded, whether you add the script in the head or before your'e closing body tag is up to you, but adding before your closing body tag will have a miniscule performance improvement, regardless as long as it's wrapped in window.onload = function(){}; 您必须先确保页面已加载,然后再运行脚本,因为要在完全加载DOM之前尝试访问DOM,无论是将脚本添加到头部还是关闭主体标记取决于您,而是添加到您的脚本之前只要封闭在body标签中,只要将其包装在window.onload = function(){}; ,它的性能都会有所提高window.onload = function(){}; , your good to go. ,您很高兴。

http://jsfiddle.net/G58KY/ http://jsfiddle.net/G58KY/

from your edit you want something like http://jsfiddle.net/YsnJv/ specify which link you are trying to access as "link" most likeyly will not work unless by some chance the link has the same id 从您的编辑中,您想要类似http://jsfiddle.net/YsnJv/的内容 ,除非您有可能使链接具有相同的ID,否则将您尝试访问的链接称为“链接”是最不可能的

window.onload = function () {
    var link = document.getElementById("link").href;
    document.getElementById("gamepage_header").innerHTML = '<h1><a href="'+link+'">Test</a></h1>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM