如何根据解析用户命令字符串为 HTML 元素着色
我正在研究一些解析来为对象着色。 例如,您可以输入 red:Hi!: 和“Hi!”会是红色的。
这是我不工作的代码:
<script type="text/javascript">
function post()
{
var preview = document.getElementById("preview");
var submit = document.getElementById("post");
var text = submit.value;
<?php str_replace("red:*:",'<i class="red">*</i>',text); ?>
preview.value = text;
}
</script>
I'm working on a little parsing thing to color objects.
For an example, you could type red:Hi!: and "Hi!" would be red.
This is my not working code:
<script type="text/javascript">
function post()
{
var preview = document.getElementById("preview");
var submit = document.getElementById("post");
var text = submit.value;
<?php str_replace("red:*:",'<i class="red">*</i>',text); ?>
preview.value = text;
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里至少有两个大问题。
你不能像你一样用通配符进行str_replace(你使用的星号只是星号字符,而不是占位符)。
您对页面渲染过程的想法是错误的 - 您不能只在 JavaScript 中调用一些 PHP 代码并让它更新页面。当您的页面在服务器上生成时,任何 PHP 代码都会被执行并打印 - 它不能像 JavaScript 那样与页面交互(JS 可以,因为它是在浏览器中执行的,但浏览器实际上永远不会像您一样看到您的 PHP 代码)可以通过转到“查看”->“源”并查看您所看到的内容进行检查)。您当然不能从 PHP 引用 JavaScript 变量。
两个选择。
选项 1 - 正确的服务器端
如果您想根据帖子在页面加载时为对象着色,请执行以下操作:
选项 2 - 正确的客户端
在您的 中包含 jQuery标签让您的生活更轻松。如果您确实不想包含 jQuery,您仍然可以更改对 getElementById 等的 jQuery 调用(我认为您需要用“.innerhtml”替换 html() 调用 - 只需查找即可)。
You have at least two massive problems here.
You can't str_replace with wildcards like you are (the asterisks you use are just that - the asterisk character, not a placeholder).
Your idea of the page-rendering process is off - you can't just call some PHP code in JavaScript and have it update the page. Any PHP code will be executed and printed when your page is generated on the server - it can't interact with the page like JavaScript can (JS can because it is executed within the browser, but the browser never actually sees your PHP code as you can check by going to View->Source and seeing what you see). You certainly cannot reference a JavaScript variable from PHP.
Two options.
Option 1 - Proper Server-Side
if you want to colour objects on page load based on post, do something like this:
Option 2 - Proper Client-Side
Include jQuery in your <head> tag to make your life easier. If you really don't want to include jQuery you can still change the jQuery calls to your getElementById etc. (you'll want to replace the html() call with '.innerhtml' I think - just look it up).
您在这里混合了服务器和客户端技术。 php 锁中的代码被评估一次(同时仍在服务器上)。您正在寻找完全在客户端运行的东西。
这意味着您需要研究 Javascript 正则表达式,而不是 PHP preg_match 类型的东西。
http://www.regular-expressions.info/javascriptexample.html
您是寻找这种类型的东西:
stringObject.replace(regularExpressionVarOrLiteral, replacement );
乔什
You're mixing server and client side technologies here. The code in the php lock is evaluated once (while still on the server). You're looking for something that will operate entirely on the client side.
This means you need to look into Javascript regular expressions, instead of PHP preg_match type stuff.
http://www.regular-expressions.info/javascriptexample.html
You're looking for this type of thing:
stringObject.replace( regularExpressionVarOrLiteral, replacement );
Josh