-2

Here is the test code:

var current_page= 0;
$('#background_music').append('<img id="bm'+current_page+'" src="success.png"   width="68px" height= "68px"/>');
$("#"+ "bm'+current_page+'").offset({top: 171, left: 41});

In this example, it doesn't work since the image won't give the expected offset. What's wrong with this:

"bm'+current_page+'"

However, if I change this to

$("#"+ "bm"+current_page).offset({top: 171, left: 41});

It works.

  1. What is the difference?
  2. Doesn't the code " bm'+current_page+' " generate a string: "bm0"?

Update: 1. What's the difference? I just can't figure it out this +variable+. In my past question, I have posted such a similar one. However, I didn't figure it out after all...

4

4 回答 4

2

It should be

$("#bm" + current_page)

instead of

$("#"+ "bm'+current_page+'")

The following is just one string:

"bm'+current_page+'"

The string delimiter here " (double quote), so nested single quotes (') here are just nothing else than characters. The string ends by the last ".

However, In

'<img id="bm' + current_page + '" src="success.png" width="68px" height="68px"/>'

the string delimiter is ' (single quote) which means that nested double quotes (") are mere characters ending up in a concatenation of two strings.

于 2012-12-26T14:03:46.220 回答
1

@Stallman 这应该可以帮助您了解报价的工作原理http://www.quirksmode.org/js/strings.html

于 2012-12-26T14:16:11.377 回答
0
$("#"+ "bm'+current_page+'").offset({top: 171, left: 41});

我认为问题出在这一行。我认为应该是:

$("#bm" + current_page).offset({top: 171, left: 41});
于 2012-12-26T14:07:13.390 回答
0

It's this line that is wonky:

$("#"+ "bm'+current_page+'").offset({top: 171, left: 41});

Try the following, we're fixing the mixed up quotes.

$('#bm'+current_page).offset({top: 171, left: 41});
于 2012-12-26T14:04:56.780 回答