【问题标题】:jQuery remodal popup adding hashtag href to urljQuery remodal 弹出窗口将标签href添加到url
【发布时间】:2019-08-22 15:51:25
【问题描述】:

我有一段html,基本上是这样的:

<a id="popap" style="display:none;" href="#modal">Call the modal with data-remodal-id="modal"</a>

然后是remodal弹出的定义如下:

<div class="remodal" style="width:60%" data-remodal-id="modal">
    <div style="width:100%; text-align:center;">
        <canvas class="canvas" height="160"></canvas>
    </div>
    <div class="hideit">
        <button data-remodal-action="close" class="remodal-close"></button>
        <div class="row">
            <div class="modalMainTitle">
                <i class="far fa-save fa-3x"></i>
                <h1>Add to search List</h1>
            </div>
            <p>
                Comment:
            </p>
            <textarea id="TextArea1" rows="2" cols="20"></textarea>
        </div>
        <div class="row" style="padding:0 41.64% ;">
            <p>Rating: </p>
            <fieldset class="rating" id="ratingSystem">
                <input type="radio" id="star5" name="rating" value="5" autocomplete="off" /><label class="full" for="star5" title="Awesome - 5 stars"></label>
                <input type="radio" id="star4" name="rating" value="4" autocomplete="off" /><label class="full" for="star4" title="Pretty good - 4 stars"></label>
                <input type="radio" id="star3" name="rating" value="3" autocomplete="off" /><label class="full" for="star3" title="Meh - 3 stars"></label>
                <input type="radio" id="star2" name="rating" value="2" autocomplete="off" /><label class="full" for="star2" title="Kinda bad - 2 stars"></label>
                <input type="radio" id="star1" name="rating" value="1" autocomplete="off" /><label class="full" for="star1" title="Sucks big time - 1 star"></label>
            </fieldset>

        </div>
        <div class="row">
            <div class="col-lg-push-3 col-lg-6">
                <div id="infoError2" style="width:100%;text-align:center; margin-right:50px !important; padding-right:45px;" class="alert"></div>
            </div>

        </div>


        @* 0 - User watchlist //  *@
        <br>
        <button data-remodal-action="cancel" class="remodal-cancel" id="cancelButton">Cancel</button>
        <button class="remodal-confirm btnSaveWatchlist">Save</button>
    </div>
</div>

然后一旦我调用要执行的弹出窗口,如下所示:

 $(document).on("click", ".btnWatchList", function (e) {
            $("#popap")[0].click();
    });

此事件完成后,我注意到它会在我所在的 URL 中添加一个#modal(我不希望这种情况发生)。

一旦事件被触发,我就会有一个如下所示的网址:

example.com?Ids=12312312412&Date=30#modal

但网址应保持如下:

example.com?Ids=12312312412&Date=30

有什么办法可以防止这种情况发生(这样一旦弹出窗口出现,#modal 部分就不会添加到 url 中)?

【问题讨论】:

  • 大多数模态插件都有一个不需要使用数据属性的 open 方法
  • github.com/VodkaBears/Remodal#hashtracking - 不确定这是否是您使用的正确插件,但正如 charlietfl 所说,它应该是一个可用的选项。
  • 这个是可以做到的,下面有一个答案指向了方法(但目前是错误的),但是我不得不问为什么?这是链接的正常行为其中包含哈希引用。
  • 我投票决定将此问题作为题外话结束,因为对于实际错误没有相关解释。

标签: javascript jquery html hashtag remodal


【解决方案1】:

您也可以按照@charlietfl 的建议使用库提供给您的方法:

 <a data-remodal-target="modal" id="popap" style="display:none;" href="#">Call the modal with data-remodal-id="modal"</a>

【讨论】:

  • 好的,我已经尝试过了并添加了:window.REMODAL_GLOBALS = { NAMESPACE: 'modal', DEFAULTS: { hashTracking: false } }; TJBlackman 建议的部分代码......但不起作用:/
  • 它仍然添加了#modal :/ meh
【解决方案2】:

对第一个答案表示歉意,我误读了这个问题,并且已经删除了该条目(道歉,这是新的但试图改进:) -)。

event.preventDefault() 可用于防止方法触发。 https://www.w3schools.com/jsref/event_preventdefault.asp

这可以附加到您的锚元素以防止修改 href。如下所示:

$('#popap').click(function(e){
     e.preventDefault();
}

希望以上内容对第一个答案有所帮助并道歉:)

【讨论】:

  • Np @J_C =D 非常感谢您的帮助...也试过这个,但也不起作用 xD
猜你喜欢
  • 2014-08-08
  • 2020-12-15
  • 1970-01-01
  • 2017-01-01
  • 2019-01-11
  • 1970-01-01
  • 2018-10-14
  • 1970-01-01
  • 2019-10-16
相关资源
最近更新 更多