在我寫的一個javascript類中,我使用了一個特定的動態加載版本的JQuery(版本3.2.1)。 我在兼容模式下使用該動態加載的版本。
我不知道在兼容模式下 "生成 "的選擇器,并作為引數傳遞給一個函式,是否也會使用兼容模式(我自己的版本而不是原始版本)。
這里的代碼:
function toggleFunction ( jQuerySelectorItem )
{
/hide all
$jq_321(".class").hide() 。
var checkBoxes = $jq_321(".class[rel-data='" jQuerySelectorItem. attr("rel-data") "'] input"); //<<<< is this 'jQuerySelectorItem' still use jQuery 3. 2.1,如果由'$jq_321'選擇器生成?
if (jQuerySelectorItem.is(" :checked"))
{
checkBoxes.toArray().forEach ( item => this. __changeDependantCheckBoxStatus(item, true))。
}
else; }
{
checkBoxes.toArray().forEach ( item => this. __changeDependantCheckBoxStatus(item, false))。
}
$jq_321(".class[rel-data='"/span> jQuerySelectorItem. attr("rel-data") "' ]").show()。
下面是我在使用JQuery之前,在兼容模式下動態加載正確的JQuery版本的方法:
window.onload = (event) => /span> {
var jqScriptTag = document.createElement("script") 。
jqScriptTag.src = 'https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'。
jqScriptTag.onload = function() {
console.log('Temporary jQuery version : ' jQuery. fn.jquery)。)
$jq_321 = jQuery.noConflict(true)。
console.log('Restored original jQuery version : ' jQuery. fn.jquery)。)
//other code that will indeed call toggleFunction when needed。
};
document.head.appendChild(jqScriptTag)。
};
這里是控制臺日志 :
臨時的jQuery版本:3.2.1
。恢復的原始jQuery版本:1.4.2
。
uj5u.com熱心網友回復:
.fn.是一種在沒有jquery物件的情況下訪問jquery方法的方式,也就是你所說的選擇器 "生成"。
你也可以在任何jquery物件上呼叫.fn.之后的部分,例如:
console. log($("div").jquery)/code
<script src="https://cdnjs. cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>/span>
<div></div>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
所以你可以很容易地檢查你的jquery物件(生成的選擇器)是哪個版本:
>。console.log(d331.jquery)
console.log(d123.jquery)
< div id="div1"></div>
<script src="https://cdnjs. cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>/span>
<script>
var d331 = $("#div1") 。
</script>>
<script src="https://cdnjs. cloudflare.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>/span>
<script>
var d123 = $("#div1") 。
</script>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
只要你的函式使用兩者都存在的jquery方法,你就不會得到一個問題(在你的函式中)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/333754.html
標籤:
上一篇:如何懸停包含特定資料的<td>?
