2017-08-04 268 views
0

實例之前的HTML元素,這是我的內容如何隱藏點擊按鈕(WordPress的)

<iframe width="560" height="315" src="https://www.youtube.com/embed/H76_oByaDZk" frameborder="0" allowfullscreen></iframe> 

    Game of Thrones S7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXX 

我的代碼工作,單擊按鈕,然後加載的iframe,但我想隱藏的iframe我的HTML元素。

這是我的single.php

$id = md5(uniqid(mt_rand(), true)); 
$text = the_content_devturkeli('<p class="serif">Full Text &raquo;</p>'); 
$last = preg_replace('^<iframe(.*?)></iframe>^', '<a href="#" onclick="showBlock(\'' . $id . '\')">Click To Watch</a> <div style="display: none;" id="' . $id . '"><embed\\1>\\2</embed><a href="#" onclick="hideBlock(\'' . $id . '\')"> Hide</a></div>', $text); 
unset($id); 
echo $last; 

點擊按鈕後,在視頻加載,但我不能隱藏的HTML源代碼

我嘗試了很多,但我不知道該怎麼做。當你回答這個問題時,請告訴我在添加代碼時,我應該在哪裏添加代碼(function.php或smt),導致網站無法正常工作。祝你有美好的一天 。

而且我改變那些

WP-包括/-的template.php後

function the_content_devturkeli($more_link_text = null, $stripteaser = 0, $more_file = '') { 
     $content = get_the_content($more_link_text, $stripteaser, $more_file); 
     $content = apply_filters('the_content', $content); 
     $content = str_replace(']]>', ']]&gt;', $content); 
     return $content; 
} 

而且header.php中

<script type="text/javascript"> 
function showBlock(blockId) { 
    document.getElementById(blockId).style.display = "block"; 
} 
function hideBlock(blockId) { 
    document.getElementById(blockId).style.display = "none"; 
} 
</script> 

哪一部分應該怎麼編輯這些。我想隱藏html源代碼直到點擊

這部分內容不是全部內容。

<iframe width="560" height="315" src="https://www.youtube.com/embed/H76_oByaDZk" frameborder="0" allowfullscreen></iframe> 
+0

從你的代碼中,有什麼想在echo $ last中回顯; ? – maulik

+0

內容(iframe和文本),但我不想顯示iframe代碼html源代碼直到點擊按鈕。 –

回答

0

要隱藏結果html源代碼,直到點擊你想要的按鈕,你需要使用ajax。所以在你的按鈕點擊函數調用你的ajax並從那個ajax調用返回你想要的html代碼(iframe代碼在你的情況下)並把這些代碼放到你的html中(使用jQuery你可以使用html(returned_iframe_code),append(returned_iframe_code)或prepend (returned_iframe_code)等功能)或javascript。

我沒有提供任何代碼,因爲您可以隨心所欲地實現ajax代碼。這是您可以實現您的要求的想法。

謝謝。

+0

我試過但我不能,我不知道如何在wordpress中使用ajax,以及如何整合我的代碼我正在搜索近1個月,但我只是在主頁上找到ajax加載內容,如加載更多按鈕或不定式滾動 –