My original question is like:
Why are the Euler-Lagrange equations invariant if we add a surface term to the action?
And there is an answer by Javier:
I have some questions about Javier's answer, but as a new user I can not comment it so I have to ask this question.
In Javier's answer he said:
Suppose you have a lagrangian $L_0$ and add a divergence to get $L=L_0+∂_μJ^μ$. Recall that the action is (in your favorite number of dimensions): $$S=∫dx L=S_0+∫dx ∂_μJ^μ=S0+∫dS n_μJ^μ$$ Here $S_0$ is the integral of $L_0$, and $n_μ$ the normal vector to your boundary. The equations of motion are the condition that $δS=0$ to first order whenever we make a variation in $L$. So: $$δS=δS_0+∫dS n_μδ(J^μ)$$ But $J_μ$ is constructed out of the fields for which you want the equations of motion. Since by hypothesis the variation of the fields at the boundary is zero, so is the variation of $J_μ$. The last term vanishes, and we get $δS=δS_0$.
But $$\delta J^{\mu }=\frac{\partial J^{\mu }}{\partial \phi }\delta \phi +\frac{\partial J^{\mu }}{\partial(\partial_{\nu}\phi)}\delta (\partial_{\nu}\phi)$$ and we only have $\delta \phi=0$ in the boundary, and generally we don't have $\delta (\partial_{\nu}\phi)=0$, so why the variation of $J_μ$ is zero?
When I found the answer of my original question, I found in some book the author writes $J^{\mu }$ as $J^{\mu }=J^{\mu }(\phi(x),x)$ , that is $J^{\mu }$ is only a function of $\phi(x)$ and $x$ but not of $\partial_{\nu}\phi$. When we write $J^{\mu }$ in this way we have: $$\delta J^{\mu }=\frac{\partial J^{\mu }}{\partial \phi }\delta \phi $$ and the variation of $J_μ$ is zero due to $\delta \phi=0$ in the boundary. And we can also prove that: $$\left(\frac{\partial }{\partial \phi }-\partial _{\nu }\frac{\partial }{\partial(\partial_{\nu}\phi)}\right)(∂_\mu J^\mu)=0$$ so we have: $$\frac{\partial L}{\partial \phi }-\partial _{\mu }\frac{\partial L}{\partial(\partial_{\mu}\phi)}=\frac{\partial L_0}{\partial \phi }-\partial _{\mu }\frac{\partial L_0}{\partial(\partial_{\mu}\phi)}$$ so the equation of motion is invariant.
But in some books the author dose not mention the condition $J^{\mu }=J^{\mu }(\phi(x),x)$, so is this condition a necessary condition that the variation of a surface term is zero?