[英]Using PHP to insert a HTML tag
I have this HTML tag (body): 我有这个HTML标记(正文):
<body onload="startTime()" background="src/bg3.png">
and I was trying to change background on refresh so I replaced the HTML tag with this PHP code: 并且我试图在刷新时更改背景,因此我用以下PHP代码替换了HTML标签:
<?php
$id = rand(1, 7);
echo "<body onload=".'"'."startTime()".'"'."background=".'"'."src/bg". $id . ".png".'"'.'>';
?>
which returns: 返回:
<body onload="startTime()" background="src/bg5.png">
where the number after bg is a random between 1 and 7. 其中bg之后的数字是1到7之间的随机数。
But the problem is that when I inspect element the body tag is simply: 但是问题是,当我检查元素时,body标签很简单:
<body>
Does PHP only allow HTML code insertion if it's being returned by a function? 如果函数返回了PHP,则只允许插入HTML代码吗?
seems you have some problem with quotes .. 似乎您在报价方面有问题..
<?php
$id = rand(1, 7);
echo "<body onload='startTime()' background='src/bg". $id . ".png'>";
?>
Try: 尝试:
echo "<body onload='startTime()' style='background:url('src/bg{$id}.png')>";
Your sting was hard to follow but I think you messed up the concatenation. 你的刺痛很难听,但我认为你搞砸了串联。
Background is part of a style tag. 背景是样式标签的一部分。 I made the Id part of string interpolation rather than more concates and putting single quotes in double works.
我将Id用作字符串插值的一部分,而不是更加简洁,并将单引号放在双引号中。
Decided to try using a function again (already tried but didn't work): 决定再次尝试使用功能(已尝试但不起作用):
function changeBG() {
//generate a random number from min to max
var min = 1, max = 7;
var str1 = "src/bg", str2 = ".png"
var number = Math.floor(Math.random() * max) + min;
var path = str1.concat(number);
path = path.concat(str2);
document.body.background = path;
}
This solved it, thanks to everybody that helped. 感谢所有人的帮助,这个问题得以解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.