javascript 我可以使用 document.getElementById.write(); 在heredoc里面写?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19406504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I use document.getElementById.write(); to write inside of heredoc?
提问by Jordan
I want to be able to edit something inside of a heredoc syntax. Something like this:
我希望能够在 heredoc 语法中编辑某些内容。像这样的东西:
index.php:
索引.php:
$var = <<<HTML
<form action="index.php" method="get" id="forma">
<input type="radio" name="choice" value="value">Message<br>
</form>
HTML;
...
$form = $var;
js:
js:
<script>
document.getElementById('forma').open();
document.getElementById('forma').write('<input type="submit">');
document.getElementById('forma').close();
</script>
EDIT: My goal is to have a button to go to a new page, but the button won't be present until you click OK on a JS confirm()
popup.
编辑:我的目标是有一个按钮可以转到新页面,但是直到您在 JSconfirm()
弹出窗口上单击“确定”后,该按钮才会出现 。
回答by Tibos
What you are trying to do (change the HEREDOC) is impossible.
您尝试做的事情(更改 HEREDOC)是不可能的。
The PHP gets interpreted on the server and the result is a HTML file with some embedded JS. Only after this HTML file gets to the client and is interpreted, the JS is executed. At this point the original PHP file containing the HEREDOC is long gone.
PHP 在服务器上被解释,结果是一个带有一些嵌入式 JS 的 HTML 文件。只有在这个 HTML 文件到达客户端并被解释后,才会执行 JS。此时,包含 HEREDOC 的原始 PHP 文件早已不复存在。
What you can do however is manipulate the DOM on the client side, but you should look to element.innerHTML
as an alternative to document.write
.
但是,您可以做的是在客户端操作 DOM,但您应该将其element.innerHTML
视为document.write
.
回答by lethal-guitar
If I understand correctly, you'd like to have a button which becomes visible after some user action. I'd suggest creating the button hidden and then displaying it via JS, instead of creating it via JS.
如果我理解正确,您希望有一个按钮,该按钮在某些用户操作后变得可见。我建议创建隐藏的按钮,然后通过 JS 显示它,而不是通过 JS 创建它。
So your HTML becomes:
所以你的 HTML 变成:
$var = <<<HTML
<form action="index.php" method="get" id="forma">
<input type="radio" name="choice" value="value">Message<br>
<!-- Note the *style* attribute -->
<input type="submit" style="display: none;" id="submitBtn">
</form>
HTML;
And then in your JS, you make the button visible as soon as your confirm()
call succeeds:
然后在您的 JS 中,一旦您的confirm()
调用成功,您就使按钮可见:
function someJsHandler() {
if (confirm("Your message here")) {
var button = document.getElementById('submitBtn');
button.style.display = 'block'; // Makes the button visible
}
}
Edit:JsFiddle for this: http://jsfiddle.net/bQ6Un/
编辑:JsFiddle 为此:http: //jsfiddle.net/bQ6Un/