2015-06-16 9 views
99

निम्नलिखित कोड क्रोम में एक समस्या के बिना चलाया जा सकता है में ठीक काम करता है, लेकिन इंटरनेट एक्सप्लोरर में निम्न त्रुटि 11.कोड IE 11 में नहीं चल रहा, क्रोम

वस्तु संपत्ति या विधि का समर्थन नहीं करता फेंकता 'शुरू होता है'

मैं एक चर में तत्व की आईडी संग्रहीत कर रहा हूं। मामला क्या है?

function changeClass(elId) { 
 
    var array = document.getElementsByTagName('td'); 
 
    
 
    for (var a = 0; a < array.length; a++) { 
 
    var str = array[a].id; 
 
    
 
    if (str.startsWith('REP')) { 
 
     if (str == elId) { 
 
     array[a].style.backgroundColor = "Blue"; 
 
     array[a].style.color = "white"; 
 
     } else { 
 
     array[a].style.backgroundColor = ""; 
 
     array[a].style.color = ""; 
 
     } 
 
    } else if (str.startsWith('D')) { 
 
     if (str == elId) { 
 
     array[a].style.backgroundColor = "Blue"; 
 
     array[a].style.color = "white"; 
 
     } else { 
 
     array[a].style.backgroundColor = ""; 
 
     array[a].style.color = ""; 
 
     } 
 
    } 
 
    } 
 
}
<table> 
 
    <tr> 
 
    <td id="REP1" onclick="changeClass('REP1');">REPS</td> 
 
    <td id="td1">&nbsp;</td> 
 
    </tr> 
 
    <tr> 
 
    <td id="td1">&nbsp;</td> 
 
    <td id="D1" onclick="changeClass('D1');">Doors</td> 
 
    </tr> 
 
    <tr> 
 
    <td id="td1">&nbsp;</td> 
 
    <td id="D12" onclick="changeClass('D12');">Doors</td> 
 
    </tr> 
 
</table>

+8

आपको इसके बजाय IE के लिए 'str.indexOf ("REP") == 0' का उपयोग करने की आवश्यकता होगी। –

+0

ES6 अभी तक मानक नहीं है https://kangax.github.io/compat-table/es6/ संक्रमण की सहायता के लिए एक ES6 शिम लाइब्रेरी है https://github.com/paulmillr/es6-shim/ बस इसी तरह की ES5 (सब कुछ शामिल नहीं है) – Xotic750

उत्तर

211

String.prototype.startsWith जावास्क्रिप्ट, ES6 का नवीनतम संस्करण में एक मानक तरीका है।

नीचे संगतता तालिका को देखते हुए, हम देख सकते हैं कि यह इंटरनेट एक्सप्लोरर के संस्करणों को छोड़कर सभी मौजूदा प्रमुख प्लेटफ़ॉर्म, पर समर्थित है।

╔═══════════════╦════════╦═════════╦═══════╦═══════════════════╦═══════╦════════╗ 
║ Feature ║ Chrome ║ Firefox ║ Edge ║ Internet Explorer ║ Opera ║ Safari ║ 
╠═══════════════╬════════╬═════════╬═══════╬═══════════════════╬═══════╬════════╣ 
║ Basic Support ║ 41+ ║  17+ ║ (Yes) ║ No Support  ║ 28 ║  9 ║ 
╚═══════════════╩════════╩═════════╩═══════╩═══════════════════╩═══════╩════════╝ 

आपको .startsWith स्वयं को लागू करने की आवश्यकता होगी। यहाँ polyfill है:

if (!String.prototype.startsWith) { 
    String.prototype.startsWith = function(searchString, position) { 
    position = position || 0; 
    return this.indexOf(searchString, position) === position; 
    }; 
} 
10

text.indexOf("newString")startsWith के बजाय सबसे अच्छा तरीका है।

उदाहरण:

var text = "Format"; 
if(text.indexOf("Format") == 0) { 
    alert(text + " = Format"); 
} else { 
    alert(text + " != Format"); 
} 
+0

यह सबसे अच्छा तरीका क्यों है? – TylerH

+0

indexOf विधि प्रारंभ के लिए प्रतिस्थापन में नहीं है, इंडेक्स अगर मूल्य दिया गया है तो यह मूल्य वापस करेगा यदि यह किसी भी स्ट्रिंग में किसी भी स्थान से मेल खाता है, तो मैं indexOf का उपयोग करने का सुझाव नहीं देता हूं। –

+0

'indexOf' वापसी मूल्य करता है, लेकिन @ जोना ने जांच की है कि वापसी मान शून्य है (यानी स्ट्रिंग शुरुआत में है), जिसका अर्थ है * यह * शुरू होता है 'के लिए एक अच्छा प्रतिस्थापन है! – SharpC

3

यदि यह कोणीय 2 + आवेदन में हो रहा है, तो आप सिर्फ टिप्पणी हटाएंpolyfills.ts में स्ट्रिंग polyfills कर सकते हैं:

import 'core-js/es6/string'; 
1

दूसरों startsWith ने कहा है के रूप में और समाप्त होता है ईएस 6 का हिस्सा हैं और आईई 11 में उपलब्ध नहीं है। हमारी कंपनी हमेशा आईओ 11 के लिए पॉलीफिल समाधान के रूप में lodash पुस्तकालय का उपयोग करता है। https://lodash.com/docs/4.17.4

_.startsWith([string=''], [target], [position=0]) 
3

जे एस फाइल करने के लिए नीचे दिए गए कोड को जोड़ने से मेरे लिए काम किया:

if (!String.prototype.startsWith) { 
    String.prototype.startsWith = function(searchString, position) { 
    position = position || 0; 
    return this.indexOf(searchString, position) === position; 
    }; 
} 
0

ओका के पद महान काम कर रहा है, वहीं यह थोड़ा पुराना हो सकता है। मुझे पता चला कि lodash इसे एक ही फ़ंक्शन से निपट सकता है। यदि आपके पास लॉनाश स्थापित है, तो यह आपको कुछ पंक्तियों को बचा सकता है।

बस कोशिश:

import { startsWith } from lodash; 

। । ।

if (startsWith(yourVariable, 'REP')) { 
     return yourVariable;   
    return yourVariable; 
     }  
    } 
संबंधित मुद्दे