0
if ($("#canvas").css('background-image') == 'url(images/endOfGame.jpg)') {

動作しません。しかし、これは:

var element = document.getElementById('canvas');
                var style = window.getComputedStyle(element);
                var imagex = style.getPropertyValue('background-image');
                console.log(imagex);
                if (imagex === "url(file:///C:/Users/Jack/Documents/myGames/Pong/images/endOfGame.jpg)") {

これはしません:

var element = document.getElementById('canvas');
                    var style = window.getComputedStyle(element);
                    var imagex = style.getPropertyValue('background-image');
                    console.log(imagex);
                    if (imagex === "url(images/endOfGame.jpg)") {

なぜ?ゲームを実行するすべてのコンピューターの完全なファイル パス コードを変更する必要があります。良くない。

ありがとう。

4

1 に答える 1

2

indexOf見つかったテキストの文字位置 (0 以上) を返すか、見つからない場合は -1 を返す which を使用できます。

if (imagex.indexOf("url(images/endOfGame.jpg)") >= 0) {
    // yes, string contains that text
}

を好む:

if (imagex.indexOf("images/endOfGame.jpg") >= 0) {
    // yes, string contains that text
}

無視しurl(..)ます。次のバージョンでは、大文字と小文字の違い (大文字または小文字) は無視されます。

if (imagex.toUpperCase().indexOf("images/endOfGame.jpg".toUpperCase()) >= 0) {
    // yes, string contains that text
}
于 2013-07-27T23:27:10.737 に答える