doumao8803 2017-03-07 15:07
浏览 55
已采纳

将href更改为onclick

On wordpress, does anyone know how to programatically change link from:

<a href="some_url">click</a>

to:

<a onclick="window.open('some_url','_blank', 'location=no')">click</a>

so that all links created in the wordpress visual editor be opened via inappbrowser in a cordova app.

After googling around, below is the closest that i can get, but still doesn't work, the '%link%' variable doesn't change to the actual link url :

add_filter('the_content', 'changeToOnclick');
function changeToOnclick($content) {
    return preg_replace('/<a [^>]*>/', "<a onclick=\"window.open('%link%', '_blank', 'location=no')\">", $content);
}

any help will be appreciated :)

  • 写回答

2条回答 默认 最新

  • douju1365 2017-03-07 17:25
    关注

    The correct way to do exactly what you want is this:

    add_filter('the_content', 'changeToOnclick');
    function changeToOnclick($content) {     
         return preg_replace('/<a href="(.+?)">/', '<a onclick="window.open(\'$1\', \'_blank\', \'location=no\');">',$content);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换