输出到标签上的属性如果您只需要将字符串输出到标签上的属性,请使用 htmlspecialchars.假设一个变量:...你可以这样输出:<div data-nifty-attr="<?...或者如果您使用短标签:<div data-nifty-attr="<?= htmlspecialchars($nifty)?>">...</div>I currently echo certain variables in hidden input fields and read them out with Javascript whenever I need them.Me and a colleague are now thinking of generating an extra Javascript file withWhat are good ways to pass variables from 解决方案 General Data PassingA commonly used exchange format for JavaScript is JSON, using json_encode. A <?then returns a JavaScript object literal like this:{ "test" : "var", "intvalue" : 1}You can directly echo it into a JavaScript variable on your page, e.g.:var data = <?...or request it via Ajax (e.g. using jQuery's getJSON).Outputting to attributes on tagsIf you just need to output a string to an attribute on a tag, use htmlspecialchars. Assuming a variable:<?...you can output it like this:<div data-nifty-attr="<?...or if you use short tags:<div data-nifty-attr="<?= htmlspecialchars($nifty)?>">...</div> 这篇关于将