最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Calling JavaScript function into a P tag - Stack Overflow

programmeradmin3浏览0评论

I would like to call the following javaScript function so it gets outputted to a HTML P tag, but am not sure how to do this without explicitly calling the function in the HTML file.

I do not want to do this...

<p class="showcode">
<script type="text/javascript">
    wise_words();
</script>
</p>

I would like to keep the javaScript code all in one js file.

I have tried it this way but this does not seem to work...

document.getElementById("showcode").innerHTML = wise_words();

I would really appreciate any help as to what I am doing wrong.

Here is my code... I would like to have the generated text get outputted inside the grey box.

I would like to call the following javaScript function so it gets outputted to a HTML P tag, but am not sure how to do this without explicitly calling the function in the HTML file.

I do not want to do this...

<p class="showcode">
<script type="text/javascript">
    wise_words();
</script>
</p>

I would like to keep the javaScript code all in one js file.

I have tried it this way but this does not seem to work...

document.getElementById("showcode").innerHTML = wise_words();

I would really appreciate any help as to what I am doing wrong.

Here is my code... http://codepen.io/anon/pen/qfLdE I would like to have the generated text get outputted inside the grey box.

Share Improve this question asked Jun 23, 2014 at 10:59 AdamAdam 1,4598 gold badges29 silver badges48 bronze badges 8
  • what your function should do? – arnold.NET.JS Commented Jun 23, 2014 at 11:02
  • Put the definition of the function in the header, not in the p-tag, and do something like $("p").text(wise_words()); – blgt Commented Jun 23, 2014 at 11:03
  • arnold.NET.JS it should output some text, you can see at the codepen link – Adam Commented Jun 23, 2014 at 11:04
  • @blgt when you suggest to use a library like jQuery that is not mentioned in the question you should at least tell which library it is. And wise_words() is a function call and not a definition. – t.niese Commented Jun 23, 2014 at 11:08
  • @Adam you don't return anything from wise_words so why do you expect that something else then undefined should be shown? – t.niese Commented Jun 23, 2014 at 11:09
 |  Show 3 more ments

3 Answers 3

Reset to default 2

You should call the function in an onload handler, so that it is executed after the DOM has been constructed:

window.onload = function() {
    document.getElementById("showcode").innerHTML = wise_words();        
}

Another problem is that your wise_words() function is using document.write (please don't use document.write) instead of returning a value. You need to return a value:

var retText = wiseText[nextVal][0];

nextVal += 1;
writeCookie("wisewords", nextVal.toString(), 33);

return retText;

Try following using Jquery:

$(".showcode").html(wise_words());

NOTE: Assuming your function returns the HTML/text.

<p class="showcode">
    <script type="text/javascript">
        document.write(wise_words());
    </script>
</p>

or:

<body onload="document.getElementById('showcode').innerHTML = wise_words()">
    <p id="showcode">
    </p>
</body>

(note id instead of class).

发布评论

评论列表(0)

  1. 暂无评论