简体   繁体   English

将字符串值放入 <p> 来自javascript的标签

[英]putting a string value into a <p> tag from javascript

I have a program that is a three card brag game, which basically randomly selects a card using the random function to select a card from two arrays after that i've used a series of if statements decides what hand the player has. 我有一个程序,它是一个三张牌的吹牛游戏,基本上是使用随机函数从两个数组中选择一张牌,然后我使用了一系列if语句来确定玩家的手牌,然后从随机选择一张牌。

i want to be able to get a piece of text from inside that if statement in javascript into a paragraph within the HTML. 我希望能够从javascript中的if语句中获取一段文本到HTML内的一段中。

This is an example of one of my if statements that decides one of the hands. 这是决定一只手的if语句之一的示例。

if(crand >= crand1 && crand >= crand2 || 
   crand1 >= crand && crand1 >= crand2 || 
   crand2 >= crand && crand2 >= crand1) {
         document.getElementById("P1").innerHTML='Player has high card';
}

this is what i have in my HTML body where i want that information to go 这就是我在HTML正文中需要的信息

<p id='P1'></p>

Make sure that the code is in the document just before the <body> element closes ( </body ) so that the element can be found when your code executes. 确保代码位于<body>元素( </body )关闭之前的文档中,以便在执行代码时可以找到该元素。

<body>

  <p id="P1"></p>

  <script>

    // By placing your script after all the HTML elements, you 
    // ensure that they are loaded into memory by the time the
    // script runs.
    var p1 = document.getElementById("P1");

    var x = 10;

    if(x === 10){
      p1.textContent = "Hello!";
    }

  </script>
</body>

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

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