문제

I want to run a function inside a few quotes and double quotes but it's not working

document.getElementById("loggedin")
.innerHTML="welcome " + user + "<a onclick=deletecookie()> yes </a>";
도움이 되었습니까?

해결책

It should be

document.getElementById("loggedin")
    .innerHTML="welcome " + user + "<a onclick='deletecookie()'> yes </a>";

Refer to this question for why quotes should be included around HTML5 attributes.

Generally speaking though, it's better to bind something like this via a jQuery event listener within your javascript code rather than injecting it into the DOM. For example:

document.getElementById('loggedin')
    .innerHTML = "welcome " + user + "<a> yes </a>";
$('#loggedin').on('click', 'a', deletecookie);

This keeps your javascript isolated from your rendered HTML and makes it clearer how things are set up for future development.

다른 팁

Or you could use escaped double quotes, as shown below.

document.getElementById("loggedin").innerHTML="welcome " + user + "<a onclick=\"deletecookie()\"> yes </a>";
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top