混合javascript,jQuery和PHP,换行符 [英] Mixing javascript, jQuery and PHP, newline

查看:101
本文介绍了混合javascript,jQuery和PHP,换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,所以我在这里有几件事:

Ok, so I have a few things here:

JavaScript:

Javascript:

desc = "line 1 \n line 2"

jQuery:

$("#msg").text(desc);

PHP:

const NUM = 555;

我想要的是更改ID为msg<p>的文本,以便它包含一段带有许多行的文本,其中之一来自PHP常数.

What I want, is to change the text of the <p> with the id of msg, so that it would contain a piece of text with a number of lines, and in one of them the number from the PHP constant.

像这样:

第1行
2 555号线,2号线续
第3行

Line 1
Line 2 555, Line 2 continued
Line 3

我的问题是如何将它们混合在一起?我尝试了以下方法:

My problem is how do I mix them all? I tried the following:

var desc = "line 1 \n line2" + <?php echo NUM ?> +"\n line 3";,这是行不通的.

推荐答案

您的代码有几个问题:

  • PHP常数应使用define("CONSTANT_NAME", "VALUE");语法定义;
  • \n在HTML标记内无效(如果不应用white-space: pre;pre-wrap);
  • <?php echo NUM; ?>应该用"包裹,或者应该放在JavaScript字符串中;
  • $("#msg").text(desc)将从desc中删除所有标签,因此您需要改用.html(desc).
  • PHP constants should be defined using define("CONSTANT_NAME", "VALUE"); syntax;
  • \n has no effect inside HTML tag (if you dont apply white-space: pre; or pre-wrap);
  • <?php echo NUM; ?> should be wrapped with " or should be inside JavaScript string;
  • $("#msg").text(desc) will remove all tags from desc, thus you need to use .html(desc) instead.

您需要的是这样的

PHP

define("NUM", 555);

JavaScript

var desc = "line 1<br/>line2 <?php echo NUM; ?><br/>line 3";
$("#msg").html(desc);

这篇关于混合javascript,jQuery和PHP,换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆