【问题标题】:Mobile detect Innerhtml in php code functions.php手机检测php代码functions.php中的Innerhtml
【发布时间】:2015-05-25 15:22:03
【问题描述】:

在我的functions.php wordpress 文件中,我有以下代码:

function mia_on_load_script()
{
    // Not our page, do nothing
    if( !is_page( 'test' ) )
        return;
?>

    <script type="text/javascript">
function change1(){
  document.getElementById('mydiv').innerHTML = '<div id="1" style="margin-left: 0px; padding: 0px; float: left; width: 640px; height: 392px; border: 0px;"><iframe style="border: 0 none transparent;" src="//www.ustream.tv/embed/******?wmode=direct&amp;autoplay=true" width="640" height="392" frameborder="no"></iframe></div><div id="2" style="margin-left: 0px; padding: 0px; float: left; width: 320px; height: 392px; border: 0px;"><iframe style="border: 0 none transparent;" src="//www.ustream.tv/socialstream/******" width="320" height="392" frameborder="no"></iframe></div><div id="addthisc" style="margin:0px; text-align: center; background-color:#0A0A0C; paddding: 0px 0px 0px 0px; width: 640px; height: 100%;  border:0px;"><div class="addthis_native_toolbox"></div></div>';
}
</script>


    <?php   
    };        
add_action( 'wp_head', 'mia_on_load_script' );

?>

网页测试中的HTML代码如下:

<input type="button" onclick="change1()" value="change html in mydiv " />

此功能更改 div 中的 html:'mydiv' 点击按钮“更改 mydiv 中的 html”

当从移动浏览器发出请求时,我希望在加载网页时更改它,而不是更改 html 单击带有 javascript 的按钮,所以我 已决定使用 Mobile_Detect.php 类 (https://github.com/serbanghita/Mobile-Detect)

我之前的函数变成了:

function mia_on_load_script()
{
    // Not our page, do nothing
    if( !is_page( 'test' ) )
        return;
?>

         <?php   
 require_once(TEMPLATEPATH . '/MyScript/Mobile_Detect.php');
 $detect = new Mobile_Detect; 
 if ( $detect->isMobile() ) {
???????????????????????????????????????????????????????????
???????????????????????????????????????????????????????????
???????????????????????????????????????????????????????????
}

?>



    <?php   
    };        
add_action( 'wp_head', 'mia_on_load_script' );

?>

那么我必须插入哪些代码来代替问号来更改 html 来代替 'document.getElementById('mydiv').innerHTML'? 谢谢

【问题讨论】:

    标签: php mobile innerhtml detect


    【解决方案1】:

    使用window.addEventListener('load', ...):

    function mia_on_load_script()
    {
        if(is_page('test'))
        {
            require_once(TEMPLATEPATH . '/MyScript/Mobile_Detect.php');
            $detect = new Mobile_Detect; 
            if($detect->isMobile())
            {
    ?>
    
    <script> <!-- No "type" attribute for HTML 5 -->
    window.addEventListener('load', function()
    {
        document.getElementById('mydiv').innerHTML = '<div id="1" style="margin-left: 0px; padding: 0px; float: left; width: 640px; height: 392px; border: 0px;"><iframe style="border: 0 none transparent;" src="//www.ustream.tv/embed/******?wmode=direct&amp;autoplay=true" width="640" height="392" frameborder="no"></iframe></div><div id="2" style="margin-left: 0px; padding: 0px; float: left; width: 320px; height: 392px; border: 0px;"><iframe style="border: 0 none transparent;" src="//www.ustream.tv/socialstream/******" width="320" height="392" frameborder="no"></iframe></div><div id="addthisc" style="margin:0px; text-align: center; background-color:#0A0A0C; paddding: 0px 0px 0px 0px; width: 640px; height: 100%;  border:0px;"><div class="addthis_native_toolbox"></div></div>';
    });
    </script>
    
    <?php
            }
        }
    }
    add_action('wp_head', 'mia_on_load_script');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-27
      • 1970-01-01
      • 2013-08-19
      • 1970-01-01
      • 2013-10-25
      • 1970-01-01
      相关资源
      最近更新 更多