我一直在尝试使用HTML表单/选项创建一个下拉菜单。当用户单击所需的选项时,它应该在同一窗口中打开一个新选项卡。我目前正在使用一个预先制作的插件(对我的项目的设计做了一些改动,并适应了其余的代码),并设法让它几乎工作。问题是它没有在新标签中打开:它在同一页面上打开(如_self)。
下面是我正在使用的HTML代码,以及javascript文件。
我真的很难找到解决方案,但我知道这可能很容易。我尝试了几件事,但没有一件工作。
<!DOCTYPE HTML>
<html>
<head>
<!-- begin meta -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Ex. 1</title>
<!-- end meta -->
<!-- begin CSS -->
<link href="css/style_dropdown.css" type="text/css" rel="stylesheet" />
<!-- end CSS -->
<!-- begin JS -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="js/modernizr-2.0.6.min.js" type="text/javascript"></script>
<script type="text/javascript" src="js/jquery.dropdown.js"></script>
<script>
$( function() {
$( '#cd-dropdown' ).dropdown( {
gutter : 5,
stack : true,
slidingIn : 100,
onOptionSelect : function( opt ) {
window.location = opt.data( 'value' );
}
} );
});
<!-- end JS -->
<head>
<body>
<form>
<select id="cd-dropdown" class="cd-select">
<option value="-1" selected>Social</option>
<option value="https://www.facebook.com" class="icon-facebook">Facebook</option>
<option value="https://plus.google.com/" class="icon-googleplus">Google Plus</option>
<option value="https://soundcloud.com/" class="icon-soundcloud">Soundcloud</option>
<option value="https://www.tumblr.com/" class="icon-tumblr">Tumblr</option>
<option value="http://www.youtube.com/" class="icon-youtube">You Tube</option>
</select>
</form>
</body>
</html>
现在是.js文件
/**
JS DROPDOWN MENU
*/
;( function( $, window, undefined ) {
'use strict';
$.DropDown = function( options, element ) {
this.$el = $( element );
this._init( options );
};
// the options
$.DropDown.defaults = {
speed : 300,
easing : 'ease',
gutter : 0,
// initial stack effect
stack : true,
// delay between each option animation
delay : 0,
// random angle and positions for the options
random : false,
// rotated [right||left||false] : the options will be rotated to thr right side or left side.
// make sure to tune the transform origin in the stylesheet
rotated : false,
// effect to slide in the options. value is the margin to start with
slidingIn : false,
onOptionSelect : function(opt) { return false; }
};
$.DropDown.prototype = {
_init : function( options ) {
// options
this.options = $.extend( true, {}, $.DropDown.defaults, options );
this._layout();
this._initEvents();
},
_layout : function() {
var self = this;
this.minZIndex = 1000;
var value = this._transformSelect();
this.opts = this.listopts.children( 'li' );
this.optsCount = this.opts.length;
this.size = { width : this.dd.width(), height : this.dd.height() };
var elName = this.$el.attr( 'name' ), elId = this.$el.attr( 'id' ),
inputName = elName !== undefined ? elName : elId !== undefined ? elId : 'cd-dropdown-' + ( new Date() ).getTime();
this.inputEl = $( '<input type="hidden" name="' + inputName + '" value="' + value + '"></input>' ).insertAfter( this.selectlabel );
this.selectlabel.css( 'z-index', this.minZIndex + this.optsCount );
this._positionOpts();
if( Modernizr.csstransitions ) {
setTimeout( function() { self.opts.css( 'transition', 'all ' + self.options.speed + 'ms ' + self.options.easing ); }, 25 );
}
},
_transformSelect : function() {
var optshtml = '', selectlabel = '', value = -1;
this.$el.children( 'option' ).each( function() {
var $this = $( this ),
val = isNaN( $this.attr( 'value' ) ) ? $this.attr( 'value' ) : Number( $this.attr( 'value' ) ) ,
classes = $this.attr( 'class' ),
selected = $this.attr( 'selected' ),
label = $this.text();
if( val !== -1 ) {
optshtml +=
classes !== undefined ?
'<li data-value="' + val + '"><span class="' + classes + '">' + label + '</span></li>' :
'<li data-value="' + val + '"><span>' + label + '</span></li>';
}
if( selected ) {
selectlabel = label;
value = val;
}
} );
this.listopts = $( '<ul/>' ).append( optshtml );
this.selectlabel = $( '<span/>' ).append( selectlabel );
this.dd = $( '<div class="cd-dropdown"/>' ).append( this.selectlabel, this.listopts ).insertAfter( this.$el );
this.$el.remove();
return value;
},
_positionOpts : function( anim ) {
var self = this;
this.listopts.css( 'height', 'auto' );
this.opts
.each( function( i ) {
$( this ).css( {
zIndex : self.minZIndex + self.optsCount - 1 - i,
top : self.options.slidingIn ? ( i + 1 ) * ( self.size.height + self.options.gutter ) : 0,
left : 0,
marginLeft : self.options.slidingIn ? i % 2 === 0 ? self.options.slidingIn : - self.options.slidingIn : 0,
opacity : self.options.slidingIn ? 0 : 1,
transform : 'none'
} );
} );
if( !this.options.slidingIn ) {
this.opts
.eq( this.optsCount - 1 )
.css( { top : this.options.stack ? 9 : 0, left : this.options.stack ? 4 : 0, width : this.options.stack ? this.size.width - 8 : this.size.width, transform : 'none' } )
.end()
.eq( this.optsCount - 2 )
.css( { top : this.options.stack ? 6 : 0, left : this.options.stack ? 2 : 0, width : this.options.stack ? this.size.width - 4 : this.size.width, transform : 'none' } )
.end()
.eq( this.optsCount - 3 )
.css( { top : this.options.stack ? 3 : 0, left : 0, transform : 'none' } );
}
},
_initEvents : function() {
var self = this;
this.selectlabel.on( 'mousedown.dropdown', function( event ) {
self.opened ? self.close() : self.open();
return false;
} );
this.opts.on( 'click.dropdown', function() {
if( self.opened ) {
var opt = $( this );
self.options.onOptionSelect( opt );
self.inputEl.val( opt.data( 'value' ) );
self.selectlabel.html( opt.html() );
self.close();
}
} );
},
open : function() {
var self = this;
this.dd.toggleClass( 'cd-active' );
this.listopts.css( 'height', ( this.optsCount + 1 ) * ( this.size.height + this.options.gutter ) );
this.opts.each( function( i ) {
$( this ).css( {
opacity : 1,
top : self.options.rotated ? self.size.height + self.options.gutter : ( i + 1 ) * ( self.size.height + self.options.gutter ),
left : self.options.random ? Math.floor( Math.random() * 11 - 5 ) : 0,
width : self.size.width,
marginLeft : 0,
transform : self.options.random ?
'rotate(' + Math.floor( Math.random() * 11 - 5 ) + 'deg)' :
self.options.rotated ?
self.options.rotated === 'right' ?
'rotate(-' + ( i * 5 ) + 'deg)' :
'rotate(' + ( i * 5 ) + 'deg)'
: 'none',
transitionDelay : self.options.delay && Modernizr.csstransitions ? self.options.slidingIn ? ( i * self.options.delay ) + 'ms' : ( ( self.optsCount - 1 - i ) * self.options.delay ) + 'ms' : 0
} );
} );
this.opened = true;
},
close : function() {
var self = this;
this.dd.toggleClass( 'cd-active' );
if( this.options.delay && Modernizr.csstransitions ) {
this.opts.each( function( i ) {
$( this ).css( { 'transition-delay' : self.options.slidingIn ? ( ( self.optsCount - 1 - i ) * self.options.delay ) + 'ms' : ( i * self.options.delay ) + 'ms' } );
} );
}
this._positionOpts( true );
this.opened = false;
}
}
$.fn.dropdown = function( options ) {
var instance = $.data( this, 'dropdown' );
if ( typeof options === 'string' ) {
var args = Array.prototype.slice.call( arguments, 1 );
this.each(function() {
instance[ options ].apply( instance, args );
});
}
else {
this.each(function() {
instance ? instance._init() : instance = $.data( this, 'dropdown', new $.DropDown( options, this ) );
});
}
return instance;
};
} )( jQuery, window );
答案 0 :(得分:0)
首先,您无法控制它是在新窗口中打开还是在当前窗口的新选项卡中打开:这纯粹是一种浏览器设置。如果您将目标设置为_blank
,它将以新的方式打开(可能是标签,因为这是大多数现代浏览器的默认行为)。
其次,您的HTML严重受损,因为<head>
包含一个<script>
标记,该标记会打开但不会关闭。 (此外,结束<head>
标记将被写为第二个开始标记。)
第三,你的例子不是自包含的,因为如果没有附带的CSS,它就完全不可行了。
我无法确定你在新窗口或其他地方打开任何东西的位置。您的表单没有提交按钮或任何类似的明显有用的内容。但是,只需将target="_blank"
添加到开头<form>
标记即可。但/ / p>
<form target="_blank">
<select id="cd-dropdown" class="cd-select">
<option value="-1" selected>Social</option>
<option value="https://www.facebook.com" class="icon-facebook">Facebook</option>
<option value="https://plus.google.com/" class="icon-googleplus">Google Plus</option>
<option value="https://soundcloud.com/" class="icon-soundcloud">Soundcloud</option>
<option value="https://www.tumblr.com/" class="icon-tumblr">Tumblr</option>
<option value="http://www.youtube.com/" class="icon-youtube">You Tube</option>
</select>
</form>