简体   繁体   English

语法-document.write Javascript成HTML

[英]Syntax - document.write Javascript into html

I'm trying to figure out how to pass some PHP variables into Javascript so they can be called by the html. 我试图弄清楚如何将一些PHP变量传递给Javascript,以便它们可以由html调用。 Here is the orginal markup from jplayer.org that I'm trying to work with: 这是我尝试使用的jplayer.org的原始标记:

<script type="text/javascript">
//<![CDATA[
$(document).ready(function(){

    $("#jquery_jplayer_1").jPlayer({
        ready: function (event) {
            $(this).jPlayer("setMedia", {
                title: "Bubble",
                m4a: "http://jplayer.org/audio/m4a/Miaow-07-Bubble.m4a"
            });
        },
        swfPath: "../../dist/jplayer",
        supplied: "mp3",
        wmode: "window",
        useStateClassSkin: true,
        autoBlur: false,
        smoothPlayBar: true,
        keyEnabled: true,
        remainingDuration: true,
        toggleDuration: true
    });
});
//]]>
</script>

So, I'm trying to make the "title" and "m4a" dynamic as variables. 因此,我正在尝试使“ title”和“ m4a”成为动态变量。 I tried this: 我尝试了这个:

<script>
    var audio_file = '<?php echo $full_audio; ?>'; 
    var audio_title = '<?php echo $title; ?>';
</script>

<script type="text/javascript">
//<![CDATA[
$(document).ready(function(){

    $("#jquery_jplayer_1").jPlayer({
        ready: function (event) {
            $(this).jPlayer("setMedia", {
                title: document.write(audio_title);,
                m4a: document.write(audio_file);
            });
        },
        swfPath: "../../dist/jplayer",
        supplied: "mp3",
        wmode: "window",
        useStateClassSkin: true,
        autoBlur: false,
        smoothPlayBar: true,
        keyEnabled: true,
        remainingDuration: true,
        toggleDuration: true
    });
});
//]]>
</script>

But when I inspect the html, instead of the variable being added, I get the actual text 但是当我检查html而不是添加变量时,我得到了实际的文本

"title: document.write(audio_title);,
m4a: document.write(audio_file);"

I'm not overly proficient with Javascript, any advice? 我不太精通Javascript,有什么建议吗?

I don't really understand why you're using document.write( ) to set those config variables in jPlayer( ) . 我真的不明白为什么您要使用document.write( )jPlayer( )设置那些配置变量。 Have you tried just calling the Javascript variables there instead? 您是否尝试过仅在那里调用Javascript变量? Also make sure that your two variables are being set to the correct values where you're calling the PHP. 还要确保在调用PHP时将两个变量设置为正确的值。

Try this: 尝试这个:

<script>
    var audio_file = '<?php echo $full_audio; ?>'; 
    var audio_title = '<?php echo $title; ?>';
</script>

<script type="text/javascript">
//<![CDATA[
$(document).ready(function(){

    $("#jquery_jplayer_1").jPlayer({
        ready: function (event) {
            $(this).jPlayer("setMedia", {
                title: audio_title,
                m4a: audio_file
            });
        },
        swfPath: "../../dist/jplayer",
        supplied: "mp3",
        wmode: "window",
        useStateClassSkin: true,
        autoBlur: false,
        smoothPlayBar: true,
        keyEnabled: true,
        remainingDuration: true,
        toggleDuration: true
    });
});
//]]>
</script>

Just to clarify, you'll only see the values of $audio_file and $audio_title where you set them. 为了澄清$audio_file ,您将仅在设置它们的位置看到$audio_file$audio_title的值。 Within jPlayer( ) you'll only see the variable names but that's the way it should be. jPlayer( )您只会看到变量名,但这就是应该的样子。

Try the code below - 试试下面的代码-

<script type="text/javascript">
//<![CDATA[
$(document).ready(function(){
$("#jquery_jplayer_1").jPlayer({
    ready: function (event) {
        $(this).jPlayer("setMedia", {
            title: audio_title,
            m4a: audio_file
        });
    },
    swfPath: "../../dist/jplayer",
    supplied: "mp3",
    wmode: "window",
    useStateClassSkin: true,
    autoBlur: false,
    smoothPlayBar: true,
    keyEnabled: true,
    remainingDuration: true,
    toggleDuration: true
});
});
//]]>
</script>

I have just removed the document.write from line 4 & 5 below '$(document).ready(function(){' as document.write writes the variables to the page instead of assigning it to JSON. 我刚刚从$$ document.ready(function(){'下的第4和5行删除了document.write,因为document.write将变量写入页面,而不是将其分配给JSON。

PHP is a server side language and javascript is client side. PHP是服务器端语言,而javascript是客户端。 You have to store that php value in any hidden input type. 您必须将php值存储在任何隐藏的输入类型中。 And then u can access it. 然后您可以访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM