简体   繁体   English

如何动态替换元素中的文本?

[英]How do i replace text in a element dynamically?

I have a database which contains lines of text. 我有一个包含文本行的数据库。 I have a webpage that has multiple things going on in it. 我的网页上有很多事情正在进行。 I need to read a line of text from the database, display it in the center of the page, wait "x" number of seconds, then read the next line and display it in the middle of the page. 我需要从数据库中读取一行文本,将其显示在页面的中央,等待“ x”秒,然后阅读下一行并将其显示在页面的中间。 I need to do this without refreshing the web page and disturbing the rest of the events on the page. 我需要执行此操作而不刷新网页,也不会干扰页面上的其他事件。

I already have the script working where I read and assign the text to a variable. 我已经在读取和分配文本到变量的地方使用了脚本。 But, how do I have the text display in the center of the window, on top of the rest of the stuff, and then keep doing this until I have reached the end of the text lines? 但是,如何使文本显示在窗口的中心,在其余内容的顶部,然后继续执行此操作,直到到达文本行的末尾? I am not asking how to read the text. 我不是在问如何阅读文字。 I need help displaying the text in the middle, such as in a span located in the middle of the page. 我需要在中间显示文本的帮助,例如在页面中间的跨度中。

Here is the current code.. 这是当前代码。

$query = "SELECT * FROM text_lines WHERE tid='$tsid'";
$result = mysql_query($query) or exit(mysql_error());

$timepause = 6;


while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {  

    $textline = $row['text'];

?>

<script>

document.getElementById("textline").innerHTML = $textline;

</script>

<?php

}

You are dealing with three types of programming which, it seems, you might not have a clear understanding of their roles. 您正在处理三种类型的编程,似乎您可能对它们的作用没有清楚的了解。

First, you have the creation of the HTML which is sent to the browser. 首先,您将创建发送到浏览器的HTML。 This is what PHP does, and it runs on the web server. 这就是PHP的作用,它可以在Web服务器上运行。 The database code is part of this process. 数据库代码是此过程的一部分。 This has to be done all in “one shot”, you can't run some PHP code, then “wait a few seconds” on the browser end, then run some more. 这必须“一次性完成”,您无法运行一些PHP代码,然后在浏览器端“等待几秒钟”,然后再运行一些。

Then you have the CSS. 然后,您有了CSS。 This is embedded in to HTML, and instructs the browser how to display things. 它被嵌入到HTML中,并指示浏览器如何显示事物。 Making your span display in the middle of the page is accomplished with CSS. 使用CSS完成在页面中间的显示。 This is also a “one shot” action — after the browser receives all the HTML and builds the page structure but before displaying it, it goes through the page and executes the CSS to move stuff around, sets colours and fonts, etc. 这也是“一次尝试”的操作-浏览器收到所有HTML并构建页面结构之后,但在显示它之前,它会遍历页面并执行CSS来移动内容,设置颜色和字体等。

Finally you have the Javascript, which runs on the browser after the page is loaded. 最后,您有了Javascript,可在页面加载后在浏览器上运行。 This is the only part of your page that can do stuff “asynchronously”, ie, not “one shot”, but do a little of this, then wait, then do some more stuff later. 这是页面上唯一可以“异步”执行操作的部分,即不能“一次性执行”,而是先执行一些操作,然后等待,然后再执行其他操作。 Or wait for keyboard or mouse input and react to it. 或等待键盘或鼠标输入并作出反应。 Javascript lets you manipulate the CSS and the HTML, modifying it from what was originally send from the PHP script. Javascript使您可以操纵CSS和HTML,并根据PHP脚本最初发送的内容对其进行修改。

I assume you are not using AJAX, so this means that all the data that will be manipulated by Javascript needs to be there from the beginning. 我假设您没有使用AJAX,所以这意味着将要从一开始就使用Javascript处理的所有数据。 You can't have the Javascript go and tell the web server to run some more PHP and send the HTML (this is what AJAX does). 您不能让Java语言去告诉Web服务器运行更多的PHP并发送HTML(这是AJAX所做的)。

So, your PHP needs to get all your lines of text from the database and include it with the page. 因此,您的PHP需要从数据库中获取所有文本行,并将其包括在页面中。 Then the CSS needs to hide all but the first line. 然后,CSS需要隐藏除第一行以外的所有内容。 Finally the Javascript will wait a few seconds, change the CSS to hide the first line and show the next line, and repeat this process. 最后,JavaScript将等待几秒钟,更改CSS以隐藏第一行并显示下一行,然后重复此过程。

You can't use a PHP variable directly in javascript: 您不能直接在javascript中使用PHP变量:

You instead need to wrap it: 您需要包装它:

<script>

document.getElementById("textline").innerHTML = <?php echo $textline; ?>

</script>

You could put the text in a div then absolutely position the div centered and use a high z-index so it appears above all other elements. 您可以将文本放在div中,然后将div绝对居中放置,并使用较高的z索引,使其显示在所有其他元素之上。

To run code after a number of seconds you can use the setInterval() method. 要在几秒钟后运行代码,可以使用setInterval()方法。

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

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