你要:
$('a[^=http://youtube.com]').dblclick(function(e){
e.preventDefault();
// Insert video iframe, or whatever you intend to do.
});
您不想在这里只使用纯 JavaScript,否则会遇到跨浏览器问题等。如果您想手动进行检查,而不是信任 jQuery 的正则表达式,请使用:
$('a').dblclick(function(e){
var pattern = /^http\:\/\/www\.youtube\.com/;
if(pattern.test($(this).attr('href'))) // Does the href start with http://www.youtube.com/
{
e.preventDefault();
// Insert video iframe, or whatever you intend to do.
}
});
如果你真的坚持不使用 jQuery,试试这个:
function dblclick_handler(el)
{
var pattern = /^http\:\/\/www\.youtube\.com/;
if(pattern.test(el.href)) // Does the href start with http://www.youtube.com/
{
// Insert video iframe, or whatever you intend to do.
return false;
}
return true;
}
然后:
<a href="http://www.youtube.com/test/" ondblclick="dblclick_handler(this);">Click me!</a>
注意,onclick
无论如何你都应该在这里使用。