将标记的内容存储在Javascript变量中

时间:2023-02-08 15:41:36

I am learning Javascript and I am trying to learn some pretty basic stuff. Basically I have some text in a <p> tag which I want to append to a variable. However it is not working an I'm not sure why. Any help will be greatly appreciated.

我正在学习Javascript,我正在尝试学习一些非常基本的东西。基本上我在

标签中有一些文本,我想附加到变量。然而它不起作用我不知道为什么。任何帮助将不胜感激。

<html>

<head>
<script type="text/javascript" src="js/jquery.js"> </script>
<script type="text/javascript">
  var x = $('p').html();
  document.write(x);
</script>
</head>

<body>
  <p class="first">Hello World  </p>
</body>

</html>

3 个解决方案

#1


3  

Wrap your code in $.ready handler:

将代码包装在$ .ready处理程序中:

<script type="text/javascript">

$(function(){
  var x = $('p').html();
  document.write(x);
});

</script>

The ready handler fires after DOM has loaded and parsed meaning only then you can maipulate tags (or DOM).

在加载和解析DOM之后,就绪处理程序将触发,这意味着您只能处理标记(或DOM)。

#2


0  

You are running your script before The <p class="first">Hello World</p> is reached in your HTML. Put your script in the body instead just after the <p> tag:

在HTML中达到

Hello World 之前,您正在运行脚本。将您的脚本放在正文中,而不是在

标记之后:

<html>
    <head>
        <script type="text/javascript" src="js/jquery.js"> </script>
    </head>
    <body> 
        <p class="first">Hello World</p>
        <script type="text/javascript">
            var x = $('p').html();

            document.write(x);
         </script>
    </body>
</html>

You can also use jQuery's ready function like some others have said, but that's an inefficient solution since you already know at which point in the document the <p> tag is loaded. It's much better to run your script as soon as it's loaded then to wait for the whole document to load using $.ready.

您也可以像其他人所说的那样使用jQuery的ready函数,但这是一个低效的解决方案,因为您已经知道文档中的

标记被加载了。最好在加载后立即运行脚本,然后等待使用$ .ready加载整个文档。

#3


0  

I can't see anything what isn't working (example)

我看不到任何不起作用的东西(例子)

maybe you should write

也许你应该写

jQuery(document).ready(function($){
    //your javascript code
})

but if it doesn't work with that either I should remind you that document.write normally replaces the content

但如果它不起作用,我应该提醒你document.write通常会替换内容

#1


3  

Wrap your code in $.ready handler:

将代码包装在$ .ready处理程序中:

<script type="text/javascript">

$(function(){
  var x = $('p').html();
  document.write(x);
});

</script>

The ready handler fires after DOM has loaded and parsed meaning only then you can maipulate tags (or DOM).

在加载和解析DOM之后,就绪处理程序将触发,这意味着您只能处理标记(或DOM)。

#2


0  

You are running your script before The <p class="first">Hello World</p> is reached in your HTML. Put your script in the body instead just after the <p> tag:

在HTML中达到

Hello World 之前,您正在运行脚本。将您的脚本放在正文中,而不是在

标记之后:

<html>
    <head>
        <script type="text/javascript" src="js/jquery.js"> </script>
    </head>
    <body> 
        <p class="first">Hello World</p>
        <script type="text/javascript">
            var x = $('p').html();

            document.write(x);
         </script>
    </body>
</html>

You can also use jQuery's ready function like some others have said, but that's an inefficient solution since you already know at which point in the document the <p> tag is loaded. It's much better to run your script as soon as it's loaded then to wait for the whole document to load using $.ready.

您也可以像其他人所说的那样使用jQuery的ready函数,但这是一个低效的解决方案,因为您已经知道文档中的

标记被加载了。最好在加载后立即运行脚本,然后等待使用$ .ready加载整个文档。

#3


0  

I can't see anything what isn't working (example)

我看不到任何不起作用的东西(例子)

maybe you should write

也许你应该写

jQuery(document).ready(function($){
    //your javascript code
})

but if it doesn't work with that either I should remind you that document.write normally replaces the content

但如果它不起作用,我应该提醒你document.write通常会替换内容