It is found that the three-dimensional laser-driven plasma bubble and the electron injection process can be manipulated by incorporating an external magnetic field and a plasma density gradient both along the longitudinal direction. The down-ramp of a density-profile-tailored plasma increases the wavelength of the plasma wake and hence reduces its phase velocity, which helps to trigger the electron injection. While a longitudinal magnetic field induces dynamically an expanding electron density hole in the rear of the wake bubble, which tends to reduce the peak electron velocity there. Electron injection is braked as soon as the electron peak velocity is less than the phase velocity when the density hole is large enough. Consequently, the start and end positions of electron injection can be flexibly controlled, which can lead to sub-femotsecond electron bunches with the peak current of a few kilo-Ampere when a magnetic field at \(\sim 10\) Tesla level is applied.