doujiufutaog59220 2018-04-12 16:34
浏览 75

在Question2Answer中的特定帖子的第n段后面添加javascript

I am struggling with this from quite some time but cannot find any soltion. I have a really long blog-type post with lots of paragraphs that I have added as a question in my Question2Answer website. I would like to put adsense code after let's say the 3rd paragraph, but there is no way to do it without modifying the php code. I read the Q2A documentation and from what i understood, i need to change the qa_view function. This is the original code for displaying question content in qa-theme.php:

public function q_view_content($q_view)
{
    $content = isset($q_view['content']) ? $q_view['content'] : '';

    $this->output('<div class="qa-q-view-content">');
    $this->output_raw($content);
    $this->output('</div>');
}

However, I have no idea how to add the javascipt code here and to refer it to specific post id. I was thinking about adding wrapper div after a specific paragraph, but i do not know how to accomplish this, since I am very new with php. Any help will be much appreciated.

  • 写回答

1条回答 默认 最新

  • dpcj32769 2018-04-16 09:59
    关注

    I do not recommend doing this because it's confusing the browsing experience as you interfere with the reading content. I suggest you either

    • Follow StackOverflow style and put an AD banner at the top or at the side of your page
    • Or follow Quora style where you create a 'see more' button then put your AD so there is a clear separation between the content and ads.

    If you insist query your third

    child of the question using JS or JQuery, then use insertAfter, for instance:

    ?>
    var ADS = document.createElement( 'div' );
    ADS.innerHtml = ' ... the Html content of your ADS banner' ;
    var question = document.getElementById( 'question-id' );
    var paragraphe = question.querySelectorAll('p')[2];  // third 
    question.insertBefore( ADS , question.children(2).sibling );
    // sibling is used to insert rather after the paragraph
    <?php
    

    This code is to be tested and adapted to your use case then appended to the appropriate php file (may be at the end of qa-include/qa-base.php) but take the necessary precautions that it will not interfere with the overall layout.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度