2014-10-16 41 views
0

我在网站上有一些文字,我想改变使用JavaScript,因为我无法改变任何其他方式。如何使用inner html更改网页中的静态文本?

总之,该网站是奠定了喜欢这样:

...some other divs before here, body, head, etc... 
<div id="header" class="container-fluid clearfix"> 
    <div class = "hero-unit"> 
     <h1 class="title">Support Center</h1> 
...some other divs for other parts of the page... 
    </div> 
</div> 
...more divs, footer, etc... 

我不需要文字来改变点击或者类似的东西,我只是希望它是负载设置的东西比不同Support Center但我不确定是否将脚本放在正确的位置或语法错误?

我试过把它放在前后,它似乎不工作。这里是代码:

<script type="text/javascript"> 
var targetDiv = document.getElementByID("header").getElementsByClassName("hero-unit")[0].getElementsByClassName("title")[0]; 
targetDiv.innerHTML = "Please use the Knowledge Base to find answers to the most frequently asked questions or you may submit a support ticket which will be sent to your COM email account."; 
</script> 

谢谢。

+1

在Javascript的情况下,它的:'getElementById'注意小写** d **最后,你应该得到** undefined不是在JavaScript控制台中的函数**(或类似的错误,取决于浏览器) – 2014-10-16 17:49:00

+0

该死的......好吧,我修正了这个问题,然后再次尝试,仍然没有改变文本。我试图在相关的div之前和之后的脚本,没有。这个脚本应该在我的HTML文件中的这些div之前还是之后?在控制台中,如果我把脚本放在后面,我现在得到的错误是“Uncaught TypeError:无法设置未定义的default.asp?deptID = 15028:142(匿名函数)的属性'innerHTML'”,如果我之前放置它,我得到“无法读取属性'getElementsByClassName'为null”。 – 2014-10-16 17:50:59

+0

你确定你在修复@PatrickEvans发现的东西时没有破坏任何东西吗?看到小提琴:http://jsfiddle.net/fseza1ff/ - 它在这个小小的改正后按预期工作。 – 2014-10-16 17:59:46

回答

1

罗在您的页面的实际源代码中,您的页面不包含元素,其类别为title

Your actual source code

<div id="header" class="container-fluid clearfix"> 

    <div class="hero-unit"></div> 
    <div class="container-fluid clearfix"> 
     <div class="row-fluid"> 
      <div class="leftcolumn"></div> 
      <div class="rightcolumn"></div> 
     </div> 
    </div> 
</div> 

这意味着它不会存在,直到页面加载后的某个点。你需要把你的代码放在生成h1标题元素的代码之后

+0

是的,终于明白了。而且我实际上也必须更改班级名称。尽管我把它放在页面的最后,出于某种原因,我不得不把它放在附加h1的代码后面,即$('。titledesc')。appendTo('#header> DIV');”。谢谢! – 2014-10-16 18:22:01

1

在jQuery中(如果你可以使用它),你会使用类似

$("#title").text("Something else"); 
+0

感谢您的提示,这也适用。 $('#header .container-fluid.clearfix .title')。text(“Something else”);效果很好。 – 2014-10-16 18:31:27

1

它看起来像你没有得到具体的类来改变HTML

尝试querySelector像我已经做了

JS Fiddle

var targetDiv = document.querySelector('#header > .hero-unit > h1.title') 
targetDiv.innerHTML = "Please use the Knowledge Base to find answers to the most frequently asked questions or you may submit a support ticket which will be sent to your COM email account."; 
相关问题