原型是否具有jQuery之类的无冲突选项?

问题描述 投票:2回答:3

我已经尝试过使用Google搜索,但是它只提供了jQuery noconflict选项。

我会用它,但是我的网站上的jQuery非常笨重,将需要一个白色,此外,我要添加的原型代码可能是临时的,只有几行。

如果没有原型,没有冲突选项,我将如何转换以下代码,我的JavaScript代码受到限制?

// JavaScript Document
// calculate price based on quantity
function changeQty(change){
var currentQty = parseInt($F('quant')) // Where quant is the id of your quantity input field. Gets value of currentQty field

switch (change) {
    case 'add':
        currentQty += 1
        $('quant').value = currentQty
        calculate()
        break
    case 'subtract':
        if (currentQty > 1) { // only subtract if qty is greater than zero
            currentQty -= 1
            $('quant').value = currentQty
            calculate()
        }
        break
    case 'field':
        if (currentQty > 0) {
            window.setTimeout('calculate()', 500)
        }
        break
}
}
function calculate(){
var currentQty = parseInt($F('quant')) // Where quant is the id of your quantity input field. Gets value of currentQty field    
var jsnormalprice = $F('jsnormalprice') // Where jsnormalprice is the id of your hidden base price field. Gets value of base_price field    
var jsspecialprice = $F('jsspecialprice') // Where  is the id of your hidden base price field. Gets value of base_price field   

if (currentQty > 0) { // Don't want price to display if zero if customer zeros out quantity
    var new_jsnormalprice = jsnormalprice * currentQty // Calculate the price.      
    var new_jsnormalprice = new_jsnormalprice.toFixed(2) // Only allow 2 decimals. I'll let you add rounding features up or down.

    var new_jsspecialprice = jsspecialprice * currentQty // Calculate the price.        
    var new_jsspecialprice = new_jsspecialprice.toFixed(2) // Only allow 2 decimals. I'll let you add rounding features up or down.

} else { // set price back to original price
    new_jsnormalprice = jsnormalprice
    new_jsspecialprice = jsspecialprice
}   

$('jsnormalpriceshow').update(new_jsnormalprice) // Where jsnormalpriceshow is the id of your span for the echoed product price
$('jsspecialpriceshow').update(new_jsspecialprice) // Where jsspecialpriceshow is the id of your span for the echoed product price

}
javascript prototypejs
3个回答
4
投票

原型没有无冲突模式...

我已经转换了您的代码,但是我可能错过了一两个地方...

通常,$('elemID') => $('#elemID')$F('elemID') => $('#elemID').val()是我所做的。。

// JavaScript Document
// calculate price based on quantity
function changeQty(change){
var currentQty = parseInt($('#quant').val()) // Where quant is the id of your quantity input field. Gets value of currentQty field

switch (change) {
    case 'add':
        currentQty += 1
        $('#quant').val(currentQty)
        calculate()
        break
    case 'subtract':
        if (currentQty > 1) { // only subtract if qty is greater than zero
            currentQty -= 1
            $('#quant').val(currentQty)
            calculate()
        }
        break
    case 'field':
        if (currentQty > 0) {
            window.setTimeout('calculate()', 500)
        }
        break
}
}
function calculate(){
var currentQty = parseInt($('#quant').val()) // Where quant is the id of your quantity input field. Gets value of currentQty field    
var jsnormalprice = $('#jsnormalprice').val() // Where jsnormalprice is the id of your hidden base price field. Gets value of base_price field    
var jsspecialprice = $('#jsspecialprice').val() // Where  is the id of your hidden base price field. Gets value of base_price field   

if (currentQty > 0) { // Don't want price to display if zero if customer zeros out quantity
    var new_jsnormalprice = jsnormalprice * currentQty // Calculate the price.      
    var new_jsnormalprice = new_jsnormalprice.toFixed(2) // Only allow 2 decimals. I'll let you add rounding features up or down.

    var new_jsspecialprice = jsspecialprice * currentQty // Calculate the price.        
    var new_jsspecialprice = new_jsspecialprice.toFixed(2) // Only allow 2 decimals. I'll let you add rounding features up or down.

} else { // set price back to original price
    new_jsnormalprice = jsnormalprice
    new_jsspecialprice = jsspecialprice
}   

$('#jsnormalpriceshow').html(new_jsnormalprice) // Where jsnormalpriceshow is the id of your span for the echoed product price
$('#jsspecialpriceshow').html(new_jsspecialprice) // Where jsspecialpriceshow is the id of your span for the echoed product price

}

2
投票

原型没有无冲突模式。


1
投票

不幸的是,原型没有不冲突模式。

幸运的是,您不需要使用原型来选择DOM中的元素。 jQuery可以完美地做到这一点。

代替

$F('quant')
$('quant').value = currentQty
$F('jsnormalprice')
$('jsnormalpriceshow').update(new_jsnormalprice)

您可以使用jQuery等效项:

$("#quant").val()
$("#quant").val(currentQty)
$("#jsnormalprice").val()
$("#jsnormalpriceshow").text(new_jsnormalprice)

而且,请不要在字符串中评估代码。更改

window.setTimeout('calculate()', 500)     

以更自然的方式进行:

window.setTimeout(calculate, 500)     
© www.soinside.com 2019 - 2024. All rights reserved.