gpt4 book ai didi

Jquery - 如果宽度小于 767,则更改 href

转载 作者:行者123 更新时间:2023-12-01 02:46:29 24 4
gpt4 key购买 nike

我目前正在开发一个响应式网站。我开发了一个 jquery 脚本来更改链接中的 href 属性。

$("a.modal").attr("href", "https://secured.sirvoy.com/book.php?c_id=1602&h=ea3a7c9286f068fb6c1462fad233a5e0")

它工作得很好 - 但我希望它只针对屏幕宽度小于 767 像素的设备(移动设备)。

这个脚本应该可以工作,但我无法真正让它工作。

<script type="text/javascript">
$(document).ready(function(){
if ($(window).width() < 767) {
$("a.modal").attr("href", "https://secured.sirvoy.com/book.php?c_id=1602&h=ea3a7c9286f068fb6c1462fad233a5e0")
}
});
</script>

网页链接。

http://visbyfangelse.se.linweb63.kontrollpanelen.se/rum-priser/

它是我想要更改的粉红色按钮的链接。

最佳答案

实际上,问题出在代码的其余部分。真为你让我挖掘它而感到羞耻。 :P

$('a[name=modal]').click(function(e) {
//Cancel the link behavior
e.preventDefault();
// etc etc.

因此,您可以通过 PreventDefault 来阻止 href 触发。为什么不直接在这里修补您的签到:

$('a[name=modal]').click(function(e) {
//Cancel the link behavior
e.preventDefault();
if ($(window).width() < 767)
{
// I'm assuming you'd get this dynamically somehow;
location.href="https://secured.sirvoy.com/book.php?c_id=1602&h=ea3a7c9286f068fb6c1462fad233a5e0";
return;
}
// etc etc.
<小时/>

已添加 |帮助 OP 完成项目的函数全文:

$('a[name=modal]').click(function(e) {
//Cancel the link behavior

e.preventDefault();
if ($(window).width() < 767)
{
// I'm assuming you'd get this dynamically somehow;
location.href="https://secured.sirvoy.com/book.php?c_id=1602&h=ea3a7c9286f068fb6c1462fad233a5e0";
return;
}
//Get the A tag
var id = $(this).attr('href');

//Get the screen height and width
var maskHeight = $(document).height();
var maskWidth = $(window).width();

//Set height and width to mask to fill up the whole screen
$('#mask').css({'width':maskWidth,'height':maskHeight});

//transition effect
$('#mask').fadeIn(1000);
$('#mask').fadeTo("slow",0.8);

//Get the window height and width
var winH = $(window).height();
var winW = $(window).width();


//transition effect
$(id).fadeIn(2000);

});

关于Jquery - 如果宽度小于 767,则更改 href,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10187467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com