![](/img/trans.png)
[英]How to add CSS style to HTML elements created with JavaScript?
[英]How to style html elements in javascript output in php
我正在编写的代码有问题,需要一些帮助。 我正在尝试设置样式并将类和 id 附加到一些 html 元素。 这些 html 元素是 javascript 输出。 并且 javascript 代码嵌入在 php 标签中。
我已经以不同的方式尝试过这个,但我主要是对引号有问题
<?php
echo '<script>
function newLength{
document.getElementsByClassName('listarea')[0].innerHTML = "<div style='height:auto; width:auto;' class='item' id='bgitem'><span class='pull-right' onclick='myClose(this);'><i class='fa fa-times' style='cursor:pointer'></i></span>";
}
</script>';
?>
您会注意到由于class 、 id和style选项,此代码将无法运行。 我知道可以删除样式,但其他样式需要在那里。 我在 php 中使用了 while 循环,因此每个循环的脚本都不同。 处理这种情况的最佳方法是什么。 我是初学者,所以我非常感谢帮助。 提前致谢
您可以将脚本添加到<php>
块之外,这样可以更轻松地解决引用问题:
<?php
# do PHP stuff
?>
<script>
function newLength{
document.getElementsByClassName('listarea')[0]
.innerHTML = "<div style='height:auto; width:auto;' class='item' id='bgitem'><span class='pull-right' onclick='myClose(this);'> <i class='fa fa-times' style='cursor:pointer'></i></span>";
}
</script>;
使用\\
来转义它们:
echo '<script>
function newLength{
document.getElementsByClassName(\'listarea\')[0].innerHTML = "<div style=\'height:auto; width:auto;\' class=\'item\' id=\'bgitem\'><span class=\'pull-right\' onclick=\'myClose(this);\'><i class=\'fa fa-times\' style=\'cursor:pointer\'></i></span>";
}
</script>';
顺便说一句,如果你对回声使用双引号,你只需要在 div 之前转义一个
您可以将代码放在 php 标签之外。
<?php
//PHP Codes
?>
Your JS / HTML code
<?php
//PHP Codes
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.