У меня есть ссылки на класс li класса pivot-nav-item
, которые имеют href=#sometext
. Я хотел бы удалить все после #, если определенное условие выполнено. Я использую следующий код, чтобы увидеть, коснулся ли пользователь экрана, чтобы добавить класс user-is-touching
. Поэтому я хотел бы удалить все после #, если пользователь коснулся экрана.
window.addEventListener('touchstart', function onFirstTouch() {
// we could use a class
document.body.classList.add('user-is-touching');
// or set some global variable
window.USER_IS_TOUCHING = true;
// or set your app's state however you normally would
// Remove mouse-related events here
$(".o-c, .c-f, .i-c, .c-u").unbind('mouseenter').unbind('mouseleave')
// we only need to know once that a human touched the screen, so we can stop
listening now
window.removeEventListener('touchstart', onFirstTouch, false);
}, false);
window.addEventListener('touchstart', function onFirstTouch() {
// we could use a class
document.body.classList.add('user-is-touching');
// or set some global variable
window.USER_IS_TOUCHING = true;
// or set your app's state however you normally would
// Remove mouse-related events here
$(".o-c, .c-f, .i-c, .c-u").unbind('mouseenter').unbind('mouseleave')
// we only need to know once that a human touched the screen, so we can stop listening now
window.removeEventListener('touchstart', onFirstTouch, false);
}, false);
$(".o-c").click(function() {
if ($('body').hasClass('user-is-touching')) {
} else { //do something else
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<li class="pivot-nav-item"><a data-attr="o-c" class="o-c default-underline" href="#o-c" data-toggle="my-scrollspy-2">O C</a></li>
<li class="pivot-nav-item"><a data-attr="c-f" class="c-f" href="#contact-form" data-toggle="my-scrollspy-2">C F</a></li>
<li class="pivot-nav-item"><a data-attr="i-c" class="i-c" href="#i-c" data-toggle="my-scrollspy-2">I C</a></li>
</body>