2015-06-16 31 views
0

我有一個在所有瀏覽器中都能正常工作的JavaScript,但Safari瀏覽器很適合。Safari上的Javascript錯誤

的代碼非常簡單,基本上當我改變文本字段(數值)的值<b>的內容發生變化

下面的代碼

$(document).on('change', '.priceAmount', function(e) { 
e.preventDefault(); 
$((e.target).closest('div.price_info')).find('> .supplierPrice').html(
($(this).val() + 10); 
}); 

的corrisponding HTML是

<div class="price_info">   
    <input type="number" class="priceAmount">  
    <b class="supplierPrice">20</b> 
</div> 

<b>內的20由JS插入,當我進入10在輸入框中。

Safari瀏覽器給了我這個錯誤

TypeError: undefined is not a function (evaluating '(e.target).closest('div.price_info')') 

這似乎是Safari瀏覽器,當事件被觸發

回答

1

使用$(本)不接收元素,而不是e.target。建議使用parseInt函數

$(document).on('change', '.priceAmount', function(e) { 
e.preventDefault(); 
$(this).closest('div.price_info').find('.supplierPrice').html(parseInt($(this).val()) + 10); 
}); 

Fiddle

+1

額外')''後closest' – Tushar

1

你錯過了e.target$

$($(e.target).closest('div.price_info')) 
//^ 
    .find('> .supplierPrice').html(parseInt($(this).val(), 10) + 10); 

另外,您還可以使用$(this)

​​

語法錯誤在你的代碼:

額外(html方法:

$((e.target).closest('div.price_info')).find('> .supplierPrice').html(
($(this).val() + 10); 
//^