[英]Javascript onchange function doesn't work in select menu on mobile
<script type="text/javascript">
var urlmenu = document.getElementById( "rpdropdown3" );
urlmenu.remove(1);
urlmenu.onchange = function() {
window.open( this.options[ this.selectedIndex ].value, "_blank" );
var options = rpdropdown3.options;
// Look for a default selected option
for (var i=0, iLen=options.length; i<iLen; i++) {
if (options[i].defaultSelected) {
rpdropdown3.selectedIndex = i;
return;
}
}
// If no option is the default, select first or none as appropriate
selectElement.selectedIndex = 0; // or -1 for no option selected
};
</script>';
$postslist = get_posts( $args );
foreach( $postslist as $posts ){
$string .= '<option value="' . get_the_permalink($posts) . '">-- ' .get_the_title($posts).'</option> ';
}
不適用於任何移動瀏覽器(適用於計算機瀏覽器),select 下拉菜單無法按預期打開鏈接
似乎它實際上正在工作,但它被阻止為彈出窗口。 如果 window.open 不是由用戶操作(如按鈕或鏈接)觸發,則不能使用它。
foreach ($postslist as $posts) { ?>
<select id="rpdropdown" onchange="if (this.value) window.location.href=this.value">
<option value="" selected disabled>OPTION 1</option>
<option value="<?php echo get_the_permalink($posts) ?>" onchange="window.open (this.value)"> -- <?php echo get_the_title($posts) ?></option>
</select>
<?php }
在我的情況下,我使用了onchange="if (this.value) window.location.href=this.value"並且我試圖讓它在新標簽中打開。
他們在這里和其他許多帖子上談論這個問題: javascript window.open in safari
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.